

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>Scikit-Learn Exporter &mdash; Nyoka 4.2.0 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript">
          var DOCUMENTATION_OPTIONS = {
              URL_ROOT:'../',
              VERSION:'4.2.0',
              LANGUAGE:'None',
              COLLAPSE_INDEX:false,
              FILE_SUFFIX:'.html',
              HAS_SOURCE:  true,
              SOURCELINK_SUFFIX: '.txt'
          };
      </script>
        <script type="text/javascript" src="../_static/jquery.js"></script>
        <script type="text/javascript" src="../_static/underscore.js"></script>
        <script type="text/javascript" src="../_static/doctools.js"></script>
    
    <script type="text/javascript" src="../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../index.html" class="icon icon-home"> Nyoka
          

          
          </a>

          
            
            
              <div class="version">
                4.2
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <ul>
                <li class="toctree-l1"><a class="reference internal" href="../statsmodels_to_pmml.html">Statsmodels Exporter Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../keras_model_to_pmml.html">Keras Exporter Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../retinanet.html">RetinaNet Exporter Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../lgb_to_pmml.html">LightGBM Exporter Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../pre_process.html">Pre-Processing Exporter Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../skl_to_pmml.html">Scikit-Learn Exporter Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../xgboost_to_pmml.html">XGBoost Exporter Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../exponential_smoothing.html">ExponentialSmoothing Exporter Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../preprocess_nyoka.html">Nyoka's Pre-Processing Module</a></li>
                <li class="toctree-l1"><a class="reference internal" href="../enums.html">Enums Module</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../index.html">Nyoka</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../index.html">Docs</a> &raquo;</li>
        
          <li><a href="index.html">Module code</a> &raquo;</li>
        
      <li>Scikit-Learn Exporter</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for Scikit-Learn Exporter</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">__future__</span> <span class="k">import</span> <span class="n">absolute_import</span>

<span class="kn">import</span> <span class="nn">sys</span><span class="o">,</span> <span class="nn">os</span>
<span class="n">BASE_DIR</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="vm">__file__</span><span class="p">))</span>
<span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">BASE_DIR</span><span class="p">)</span>

<span class="kn">import</span> <span class="nn">PMML44</span> <span class="k">as</span> <span class="nn">pml</span>
<span class="kn">import</span> <span class="nn">pre_process</span> <span class="k">as</span> <span class="nn">pp</span>
<span class="kn">from</span> <span class="nn">datetime</span> <span class="k">import</span> <span class="n">datetime</span>
<span class="kn">import</span> <span class="nn">math</span>
<span class="kn">import</span> <span class="nn">metadata</span>
<span class="kn">from</span> <span class="nn">enums</span> <span class="k">import</span> <span class="o">*</span>

<div class="viewcode-block" id="skl_to_pmml"><span class="k">def</span> <span class="nf">skl_to_pmml</span><span class="p">(</span><span class="n">pipeline</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="o">=</span><span class="s1">&#39;target&#39;</span><span class="p">,</span> <span class="n">pmml_f_name</span><span class="o">=</span><span class="s1">&#39;from_sklearn.pmml&#39;</span><span class="p">,</span> <span class="n">model_name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">description</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Exports scikit-learn pipeline object into pmml</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    pipeline :</span>
<span class="sd">        Contains an instance of Pipeline with preprocessing and final estimator</span>
<span class="sd">    col_names : List</span>
<span class="sd">        Contains list of feature/column names.</span>
<span class="sd">    target_name : String</span>
<span class="sd">        Name of the target column. (Default=&#39;target&#39;)</span>
<span class="sd">    pmml_f_name : String</span>
<span class="sd">        Name of the pmml file. (Default=&#39;from_sklearn.pmml&#39;)</span>
<span class="sd">    model_name : string (optional)</span>
<span class="sd">        Name of the model</span>
<span class="sd">    description : string (optional)</span>
<span class="sd">        Description of the model</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    Generates a PMML object and exports it to `pmml_f_name` </span>
<span class="sd">    </span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">model</span> <span class="o">=</span> <span class="n">pipeline</span><span class="o">.</span><span class="n">steps</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span>
    <span class="k">except</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Exporter expects pipeleine_instance and not an estimator_instance&quot;</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">col_names</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">):</span>
            <span class="n">col_names</span> <span class="o">=</span> <span class="n">col_names</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
        <span class="n">ppln_sans_predictor</span> <span class="o">=</span> <span class="n">pipeline</span><span class="o">.</span><span class="n">steps</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">trfm_dict_kwargs</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">()</span>
        <span class="n">derived_col_names</span> <span class="o">=</span> <span class="n">col_names</span>
        <span class="n">categoric_values</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">()</span>
        <span class="n">mining_imp_val</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">ppln_sans_predictor</span><span class="p">:</span>
            <span class="n">pml_pp</span> <span class="o">=</span> <span class="n">pp</span><span class="o">.</span><span class="n">get_preprocess_val</span><span class="p">(</span><span class="n">ppln_sans_predictor</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">model</span><span class="p">)</span>
            <span class="n">trfm_dict_kwargs</span><span class="p">[</span><span class="s1">&#39;TransformationDictionary&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">pml_pp</span><span class="p">[</span><span class="s1">&#39;trfm_dict&#39;</span><span class="p">]</span>
            <span class="n">derived_col_names</span> <span class="o">=</span> <span class="n">pml_pp</span><span class="p">[</span><span class="s1">&#39;derived_col_names&#39;</span><span class="p">]</span>
            <span class="n">col_names</span> <span class="o">=</span> <span class="n">pml_pp</span><span class="p">[</span><span class="s1">&#39;preprocessed_col_names&#39;</span><span class="p">]</span>
            <span class="n">categoric_values</span> <span class="o">=</span> <span class="n">pml_pp</span><span class="p">[</span><span class="s1">&#39;categorical_feat_values&#39;</span><span class="p">]</span>
            <span class="n">mining_imp_val</span> <span class="o">=</span> <span class="n">pml_pp</span><span class="p">[</span><span class="s1">&#39;mining_imp_values&#39;</span><span class="p">]</span>
            
        <span class="n">PMML_kwargs</span> <span class="o">=</span> <span class="n">get_PMML_kwargs</span><span class="p">(</span><span class="n">model</span><span class="p">,</span>
                                      <span class="n">derived_col_names</span><span class="p">,</span>
                                      <span class="n">col_names</span><span class="p">,</span>
                                      <span class="n">target_name</span><span class="p">,</span>
                                      <span class="n">mining_imp_val</span><span class="p">,</span>
                                      <span class="n">categoric_values</span><span class="p">,</span>
                                      <span class="n">model_name</span><span class="p">)</span>
             
        <span class="n">pmml</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">PMML</span><span class="p">(</span>
            <span class="n">version</span><span class="o">=</span><span class="n">PMML_SCHEMA</span><span class="o">.</span><span class="n">VERSION</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
            <span class="n">Header</span><span class="o">=</span><span class="n">get_header</span><span class="p">(</span><span class="n">description</span><span class="p">),</span>
            <span class="n">DataDictionary</span><span class="o">=</span><span class="n">get_data_dictionary</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">categoric_values</span><span class="p">),</span>
            <span class="o">**</span><span class="n">trfm_dict_kwargs</span><span class="p">,</span>
            <span class="o">**</span><span class="n">PMML_kwargs</span>
        <span class="p">)</span>
        <span class="n">pmml</span><span class="o">.</span><span class="n">export</span><span class="p">(</span><span class="n">outfile</span><span class="o">=</span><span class="nb">open</span><span class="p">(</span><span class="n">pmml_f_name</span><span class="p">,</span> <span class="s2">&quot;w&quot;</span><span class="p">),</span> <span class="n">level</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span></div>


<div class="viewcode-block" id="any_in"><span class="k">def</span> <span class="nf">any_in</span><span class="p">(</span><span class="n">seq_a</span><span class="p">,</span> <span class="n">seq_b</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Checks for common elements in two given sequence elements</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    seq_a : list</span>
<span class="sd">        A list of items</span>

<span class="sd">    seq_b : list</span>
<span class="sd">        A list of items</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    Returns a boolean value if any item of seq_a belongs to seq_b or visa versa</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="nb">any</span><span class="p">(</span><span class="n">elem</span> <span class="ow">in</span> <span class="n">seq_b</span> <span class="k">for</span> <span class="n">elem</span> <span class="ow">in</span> <span class="n">seq_a</span><span class="p">)</span></div>


<div class="viewcode-block" id="get_PMML_kwargs"><span class="k">def</span> <span class="nf">get_PMML_kwargs</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">mining_imp_val</span><span class="p">,</span> <span class="n">categoric_values</span><span class="p">,</span> <span class="n">model_name</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It returns all the pmml elements.</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model : Scikit-learn model object</span>
<span class="sd">        An instance of Scikit-learn model.</span>
<span class="sd">    derived_col_names : List </span>
<span class="sd">        Contains column names after preprocessing</span>
<span class="sd">    col_names : List</span>
<span class="sd">        Contains list of feature/column names.</span>
<span class="sd">    target_name : String</span>
<span class="sd">        Name of the target column .</span>
<span class="sd">    mining_imp_val : tuple</span>
<span class="sd">        Contains the mining_attributes,mining_strategy, mining_impute_value</span>
<span class="sd">    categoric_values : tuple</span>
<span class="sd">        Contains Categorical attribute names and its values</span>
<span class="sd">    model_name : string</span>
<span class="sd">        Name of the model</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    algo_kwargs : Dictionary</span>
<span class="sd">        Get the PMML model argument based on scikit learn model object</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">skl_mdl_super_cls_names</span> <span class="o">=</span> <span class="n">get_super_cls_names</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>
    <span class="n">regression_model_names</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;LinearRegression&#39;</span><span class="p">,</span><span class="s1">&#39;LinearSVR&#39;</span><span class="p">)</span>
    <span class="n">regression_mining_model_names</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;LogisticRegression&#39;</span><span class="p">,</span> <span class="s1">&#39;RidgeClassifier&#39;</span><span class="p">,</span><span class="s1">&#39;LinearDiscriminantAnalysis&#39;</span><span class="p">,</span> \
                                        <span class="s1">&#39;SGDClassifier&#39;</span><span class="p">,</span><span class="s1">&#39;LinearSVC&#39;</span><span class="p">,)</span>
    <span class="n">tree_model_names</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;BaseDecisionTree&#39;</span><span class="p">,)</span>
    <span class="n">support_vector_model_names</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;SVC&#39;</span><span class="p">,</span> <span class="s1">&#39;SVR&#39;</span><span class="p">)</span>
    <span class="n">anomaly_model_names</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;OneClassSVM&#39;</span><span class="p">,</span><span class="s1">&#39;IsolationForest&#39;</span><span class="p">)</span>
    <span class="n">naive_bayes_model_names</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;GaussianNB&#39;</span><span class="p">,)</span>
    <span class="n">mining_model_names</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;RandomForestRegressor&#39;</span><span class="p">,</span> <span class="s1">&#39;RandomForestClassifier&#39;</span><span class="p">,</span> <span class="s1">&#39;GradientBoostingClassifier&#39;</span><span class="p">,</span>
                            <span class="s1">&#39;GradientBoostingRegressor&#39;</span><span class="p">)</span>
    <span class="n">neurl_netwk_model_names</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;MLPClassifier&#39;</span><span class="p">,</span> <span class="s1">&#39;MLPRegressor&#39;</span><span class="p">)</span>
    <span class="n">nearest_neighbour_names</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;NeighborsBase&#39;</span><span class="p">,)</span>
    <span class="n">clustering_model_names</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;KMeans&#39;</span><span class="p">,)</span>
    <span class="k">if</span> <span class="n">any_in</span><span class="p">(</span><span class="n">tree_model_names</span><span class="p">,</span> <span class="n">skl_mdl_super_cls_names</span><span class="p">):</span>
        <span class="n">algo_kwargs</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;TreeModel&#39;</span><span class="p">:</span> <span class="n">get_tree_models</span><span class="p">(</span><span class="n">model</span><span class="p">,</span>
                                                    <span class="n">derived_col_names</span><span class="p">,</span>
                                                    <span class="n">col_names</span><span class="p">,</span>
                                                    <span class="n">target_name</span><span class="p">,</span>
                                                    <span class="n">mining_imp_val</span><span class="p">,</span>
                                                    <span class="n">categoric_values</span><span class="p">,</span>
                                                    <span class="n">model_name</span><span class="p">)}</span>
    <span class="k">elif</span> <span class="n">any_in</span><span class="p">(</span><span class="n">regression_mining_model_names</span><span class="p">,</span> <span class="n">skl_mdl_super_cls_names</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">classes_</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
            <span class="n">algo_kwargs</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;RegressionModel&#39;</span><span class="p">:</span> <span class="n">get_regrs_models</span><span class="p">(</span><span class="n">model</span><span class="p">,</span>
                                                           <span class="n">derived_col_names</span><span class="p">,</span>
                                                           <span class="n">col_names</span><span class="p">,</span>
                                                           <span class="n">target_name</span><span class="p">,</span>
                                                           <span class="n">mining_imp_val</span><span class="p">,</span>
                                                           <span class="n">categoric_values</span><span class="p">,</span>
                                                           <span class="n">model_name</span><span class="p">)}</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">algo_kwargs</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;MiningModel&#39;</span><span class="p">:</span> <span class="n">get_reg_mining_models</span><span class="p">(</span><span class="n">model</span><span class="p">,</span>
                                                                <span class="n">derived_col_names</span><span class="p">,</span>
                                                                <span class="n">col_names</span><span class="p">,</span>
                                                                <span class="n">target_name</span><span class="p">,</span>
                                                                <span class="n">mining_imp_val</span><span class="p">,</span>
                                                                <span class="n">categoric_values</span><span class="p">,</span>
                                                                <span class="n">model_name</span><span class="p">)}</span>
    <span class="k">elif</span> <span class="n">any_in</span><span class="p">(</span><span class="n">regression_model_names</span><span class="p">,</span> <span class="n">skl_mdl_super_cls_names</span><span class="p">):</span>
        <span class="n">algo_kwargs</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;RegressionModel&#39;</span><span class="p">:</span> <span class="n">get_regrs_models</span><span class="p">(</span><span class="n">model</span><span class="p">,</span>
                                                           <span class="n">derived_col_names</span><span class="p">,</span>
                                                           <span class="n">col_names</span><span class="p">,</span>
                                                           <span class="n">target_name</span><span class="p">,</span>
                                                           <span class="n">mining_imp_val</span><span class="p">,</span>
                                                           <span class="n">categoric_values</span><span class="p">,</span>
                                                           <span class="n">model_name</span><span class="p">)}</span>
    <span class="k">elif</span> <span class="n">any_in</span><span class="p">(</span><span class="n">support_vector_model_names</span><span class="p">,</span> <span class="n">skl_mdl_super_cls_names</span><span class="p">):</span>
        <span class="n">algo_kwargs</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;SupportVectorMachineModel&#39;</span><span class="p">:</span>
                           <span class="n">get_supportVectorMachine_models</span><span class="p">(</span><span class="n">model</span><span class="p">,</span>
                                                           <span class="n">derived_col_names</span><span class="p">,</span>
                                                           <span class="n">col_names</span><span class="p">,</span>
                                                           <span class="n">target_name</span><span class="p">,</span>
                                                           <span class="n">mining_imp_val</span><span class="p">,</span>
                                                           <span class="n">categoric_values</span><span class="p">,</span>
                                                           <span class="n">model_name</span><span class="p">)}</span>
    <span class="k">elif</span> <span class="n">any_in</span><span class="p">(</span><span class="n">mining_model_names</span><span class="p">,</span> <span class="n">skl_mdl_super_cls_names</span><span class="p">):</span>
        <span class="n">algo_kwargs</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;MiningModel&#39;</span><span class="p">:</span> <span class="n">get_ensemble_models</span><span class="p">(</span><span class="n">model</span><span class="p">,</span>
                                                          <span class="n">derived_col_names</span><span class="p">,</span>
                                                          <span class="n">col_names</span><span class="p">,</span>
                                                          <span class="n">target_name</span><span class="p">,</span>
                                                          <span class="n">mining_imp_val</span><span class="p">,</span>
                                                          <span class="n">categoric_values</span><span class="p">,</span>
                                                          <span class="n">model_name</span><span class="p">)}</span>
    <span class="k">elif</span> <span class="n">any_in</span><span class="p">(</span><span class="n">neurl_netwk_model_names</span><span class="p">,</span> <span class="n">skl_mdl_super_cls_names</span><span class="p">):</span>
        <span class="n">algo_kwargs</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;NeuralNetwork&#39;</span><span class="p">:</span> <span class="n">get_neural_models</span><span class="p">(</span><span class="n">model</span><span class="p">,</span>
                                                          <span class="n">derived_col_names</span><span class="p">,</span>
                                                          <span class="n">col_names</span><span class="p">,</span>
                                                          <span class="n">target_name</span><span class="p">,</span>
                                                          <span class="n">mining_imp_val</span><span class="p">,</span>
                                                          <span class="n">categoric_values</span><span class="p">,</span>
                                                          <span class="n">model_name</span><span class="p">)}</span>
    <span class="k">elif</span> <span class="n">any_in</span><span class="p">(</span><span class="n">naive_bayes_model_names</span><span class="p">,</span> <span class="n">skl_mdl_super_cls_names</span><span class="p">):</span>
        <span class="n">algo_kwargs</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;NaiveBayesModel&#39;</span><span class="p">:</span> <span class="n">get_naiveBayesModel</span><span class="p">(</span><span class="n">model</span><span class="p">,</span>
                                                              <span class="n">derived_col_names</span><span class="p">,</span>
                                                              <span class="n">col_names</span><span class="p">,</span>
                                                              <span class="n">target_name</span><span class="p">,</span>
                                                              <span class="n">mining_imp_val</span><span class="p">,</span>
                                                              <span class="n">categoric_values</span><span class="p">,</span>
                                                              <span class="n">model_name</span><span class="p">)}</span>
    <span class="k">elif</span> <span class="n">any_in</span><span class="p">(</span><span class="n">nearest_neighbour_names</span><span class="p">,</span> <span class="n">skl_mdl_super_cls_names</span><span class="p">):</span>
        <span class="n">algo_kwargs</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;NearestNeighborModel&#39;</span><span class="p">:</span>
                           <span class="n">get_nearestNeighbour_model</span><span class="p">(</span><span class="n">model</span><span class="p">,</span>
                                                      <span class="n">derived_col_names</span><span class="p">,</span>
                                                      <span class="n">col_names</span><span class="p">,</span>
                                                      <span class="n">target_name</span><span class="p">,</span>
                                                      <span class="n">mining_imp_val</span><span class="p">,</span>
                                                      <span class="n">categoric_values</span><span class="p">,</span>
                                                      <span class="n">model_name</span><span class="p">)}</span>
    <span class="k">elif</span> <span class="n">any_in</span><span class="p">(</span><span class="n">anomaly_model_names</span><span class="p">,</span> <span class="n">skl_mdl_super_cls_names</span><span class="p">):</span>
        <span class="n">algo_kwargs</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;AnomalyDetectionModel&#39;</span><span class="p">:</span>
                            <span class="n">get_anomalydetection_model</span><span class="p">(</span><span class="n">model</span><span class="p">,</span>
                                                        <span class="n">derived_col_names</span><span class="p">,</span>
                                                        <span class="n">col_names</span><span class="p">,</span>
                                                        <span class="n">target_name</span><span class="p">,</span>
                                                        <span class="n">mining_imp_val</span><span class="p">,</span>
                                                        <span class="n">categoric_values</span><span class="p">,</span>
                                                        <span class="n">model_name</span><span class="p">)}</span>
    <span class="k">elif</span> <span class="n">any_in</span><span class="p">(</span><span class="n">clustering_model_names</span><span class="p">,</span> <span class="n">skl_mdl_super_cls_names</span><span class="p">):</span>
        <span class="n">algo_kwargs</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;ClusteringModel&#39;</span><span class="p">:</span>
                            <span class="n">get_clustering_model</span><span class="p">(</span><span class="n">model</span><span class="p">,</span>
                                                    <span class="n">derived_col_names</span><span class="p">,</span>
                                                    <span class="n">col_names</span><span class="p">,</span>
                                                    <span class="n">target_name</span><span class="p">,</span>
                                                    <span class="n">mining_imp_val</span><span class="p">,</span>
                                                    <span class="n">categoric_values</span><span class="p">,</span>
                                                    <span class="n">model_name</span>
                                                 <span class="p">)}</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">{}</span><span class="s2"> is not Implemented!&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">))</span>

    <span class="k">return</span> <span class="n">algo_kwargs</span></div>


<div class="viewcode-block" id="get_model_kwargs"><span class="k">def</span> <span class="nf">get_model_kwargs</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">mining_imp_val</span><span class="p">,</span> <span class="n">categoric_values</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It returns all the model element for a specific model.</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        An instance of Scikit-learn model.</span>
<span class="sd">    col_names : List</span>
<span class="sd">        Contains list of feature/column names.</span>
<span class="sd">    target_name : String</span>
<span class="sd">        Name of the Target column.</span>
<span class="sd">    mining_imp_val : tuple</span>
<span class="sd">        Contains the mining_attributes,mining_strategy, mining_impute_value</span>
<span class="sd">    categoric_values : tuple</span>
<span class="sd">        Contains Categorical attribute names and its values</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    model_kwargs : Dictionary</span>
<span class="sd">        Returns  function name, MiningSchema and Output of the sk_model object</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">model_kwargs</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">()</span>
    <span class="n">model_kwargs</span><span class="p">[</span><span class="s1">&#39;functionName&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">get_mining_func</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>
    <span class="n">model_kwargs</span><span class="p">[</span><span class="s1">&#39;MiningSchema&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">get_mining_schema</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">mining_imp_val</span><span class="p">,</span> <span class="n">categoric_values</span><span class="p">)</span>
    <span class="n">model_kwargs</span><span class="p">[</span><span class="s1">&#39;Output&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">get_output</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">target_name</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">model_kwargs</span></div>


<div class="viewcode-block" id="get_reg_mining_models"><span class="k">def</span> <span class="nf">get_reg_mining_models</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">mining_imp_val</span><span class="p">,</span> <span class="n">categoric_values</span><span class="p">,</span> <span class="n">model_name</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Creates xml elements for multi-class linear models</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        An instance of Scikit-learn model.</span>
<span class="sd">    derived_col_names : List</span>
<span class="sd">        Contains column names after preprocessing</span>
<span class="sd">    col_names : List</span>
<span class="sd">        Contains list of feature/column names.</span>
<span class="sd">    target_name : String</span>
<span class="sd">        Name of the Target column.</span>
<span class="sd">    mining_imp_val : tuple</span>
<span class="sd">        Contains the mining_attributes,mining_strategy, mining_impute_value</span>
<span class="sd">    categoric_values : tuple</span>
<span class="sd">        Contains Categorical attribute names and its values</span>
<span class="sd">    model_name : string</span>
<span class="sd">        Name of the model</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    mining_model : List</span>
<span class="sd">        Returns a Nyoka&#39;s MiningModel object</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">num_classes</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">classes_</span><span class="p">)</span>
    <span class="n">model_kwargs</span> <span class="o">=</span> <span class="n">get_model_kwargs</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">mining_imp_val</span><span class="p">,</span> <span class="n">categoric_values</span><span class="p">)</span>

    <span class="n">mining_model</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">MiningModel</span><span class="p">(</span><span class="n">modelName</span><span class="o">=</span><span class="n">model_name</span> <span class="k">if</span> <span class="n">model_name</span> <span class="k">else</span> <span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span><span class="o">**</span><span class="n">model_kwargs</span><span class="p">)</span>
    <span class="n">inner_mining_schema</span> <span class="o">=</span> <span class="p">[</span><span class="n">mfield</span> <span class="k">for</span> <span class="n">mfield</span> <span class="ow">in</span> <span class="n">model_kwargs</span><span class="p">[</span><span class="s1">&#39;MiningSchema&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">MiningField</span> <span class="k">if</span> <span class="n">mfield</span><span class="o">.</span><span class="n">usageType</span> <span class="o">!=</span> <span class="n">FIELD_USAGE_TYPE</span><span class="o">.</span><span class="n">TARGET</span><span class="o">.</span><span class="n">value</span><span class="p">]</span>
    <span class="n">segmentation</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">Segmentation</span><span class="p">(</span><span class="n">multipleModelMethod</span><span class="o">=</span><span class="n">MULTIPLE_MODEL_METHOD</span><span class="o">.</span><span class="n">MODEL_CHAIN</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">idx</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">num_classes</span><span class="p">):</span>
        <span class="n">segment</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">Segment</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">idx</span><span class="o">+</span><span class="mi">1</span><span class="p">),</span><span class="n">True_</span><span class="o">=</span><span class="n">pml</span><span class="o">.</span><span class="n">True_</span><span class="p">())</span>
        <span class="n">segment</span><span class="o">.</span><span class="n">RegressionModel</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">RegressionModel</span><span class="p">(</span>
            <span class="n">functionName</span><span class="o">=</span><span class="n">MINING_FUNCTION</span><span class="o">.</span><span class="n">REGRESSION</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
            <span class="n">MiningSchema</span><span class="o">=</span><span class="n">pml</span><span class="o">.</span><span class="n">MiningSchema</span><span class="p">(</span>
                <span class="n">MiningField</span><span class="o">=</span><span class="n">inner_mining_schema</span>
                <span class="p">),</span>
            <span class="n">Output</span><span class="o">=</span><span class="n">pml</span><span class="o">.</span><span class="n">Output</span><span class="p">(</span>
                <span class="n">OutputField</span><span class="o">=</span><span class="p">[</span>
                    <span class="n">pml</span><span class="o">.</span><span class="n">OutputField</span><span class="p">(</span>
                        <span class="n">name</span><span class="o">=</span><span class="s2">&quot;probablity_&quot;</span><span class="o">+</span><span class="nb">str</span><span class="p">(</span><span class="n">idx</span><span class="p">),</span>
                        <span class="n">optype</span><span class="o">=</span><span class="n">OPTYPE</span><span class="o">.</span><span class="n">CONTINUOUS</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                        <span class="n">dataType</span><span class="o">=</span><span class="n">DATATYPE</span><span class="o">.</span><span class="n">DOUBLE</span><span class="o">.</span><span class="n">value</span>
                        <span class="p">)</span>
                    <span class="p">]</span>
                <span class="p">),</span>
            <span class="n">RegressionTable</span><span class="o">=</span><span class="n">get_reg_tab_for_reg_mining_model</span><span class="p">(</span><span class="n">model</span><span class="p">,</span><span class="n">derived_col_names</span><span class="p">,</span><span class="n">idx</span><span class="p">,</span><span class="n">categoric_values</span><span class="p">)</span>
        <span class="p">)</span>
        <span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">!=</span> <span class="s1">&#39;LinearSVC&#39;</span><span class="p">:</span>
            <span class="n">segment</span><span class="o">.</span><span class="n">RegressionModel</span><span class="o">.</span><span class="n">normalizationMethod</span> <span class="o">=</span> <span class="n">REGRESSION_NORMALIZATION_METHOD</span><span class="o">.</span><span class="n">LOGISTIC</span><span class="o">.</span><span class="n">value</span>
        <span class="n">segmentation</span><span class="o">.</span><span class="n">add_Segment</span><span class="p">(</span><span class="n">segment</span><span class="p">)</span>

    <span class="n">last_segment</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">Segment</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">num_classes</span><span class="o">+</span><span class="mi">1</span><span class="p">),</span><span class="n">True_</span><span class="o">=</span><span class="n">pml</span><span class="o">.</span><span class="n">True_</span><span class="p">())</span>
    <span class="n">mining_flds_for_last</span> <span class="o">=</span> <span class="p">[</span><span class="n">pml</span><span class="o">.</span><span class="n">MiningField</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">&quot;probablity_&quot;</span><span class="o">+</span><span class="nb">str</span><span class="p">(</span><span class="n">idx</span><span class="p">))</span> <span class="k">for</span> <span class="n">idx</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">num_classes</span><span class="p">)]</span>
    <span class="n">mining_flds_for_last</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">MiningField</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="n">target_name</span><span class="p">,</span><span class="n">usageType</span><span class="o">=</span><span class="n">FIELD_USAGE_TYPE</span><span class="o">.</span><span class="n">TARGET</span><span class="o">.</span><span class="n">value</span><span class="p">))</span>
    <span class="n">mining_schema_for_last</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">MiningSchema</span><span class="p">(</span><span class="n">MiningField</span><span class="o">=</span><span class="n">mining_flds_for_last</span><span class="p">)</span>
    <span class="n">reg_tab_for_last</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
    <span class="k">for</span> <span class="n">idx</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">num_classes</span><span class="p">):</span>
        <span class="n">reg_tab_for_last</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
            <span class="n">pml</span><span class="o">.</span><span class="n">RegressionTable</span><span class="p">(</span>
                <span class="n">intercept</span><span class="o">=</span><span class="s2">&quot;0.0&quot;</span><span class="p">,</span>
                <span class="n">targetCategory</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">classes_</span><span class="p">[</span><span class="n">idx</span><span class="p">]),</span>
                <span class="n">NumericPredictor</span><span class="o">=</span><span class="p">[</span><span class="n">pml</span><span class="o">.</span><span class="n">NumericPredictor</span><span class="p">(</span>
                    <span class="n">name</span><span class="o">=</span><span class="s2">&quot;probablity_&quot;</span><span class="o">+</span><span class="nb">str</span><span class="p">(</span><span class="n">idx</span><span class="p">),</span>
                    <span class="n">coefficient</span><span class="o">=</span><span class="s2">&quot;1.0&quot;</span>
                <span class="p">)]</span>
            <span class="p">)</span>
        <span class="p">)</span>

    <span class="n">last_segment</span><span class="o">.</span><span class="n">RegressionModel</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">RegressionModel</span><span class="p">(</span>
        <span class="n">functionName</span><span class="o">=</span><span class="n">MINING_FUNCTION</span><span class="o">.</span><span class="n">CLASSIFICATION</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
        <span class="n">MiningSchema</span><span class="o">=</span><span class="n">mining_schema_for_last</span><span class="p">,</span>
        <span class="n">RegressionTable</span><span class="o">=</span><span class="n">reg_tab_for_last</span>
    <span class="p">)</span>
    <span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">!=</span> <span class="s1">&#39;LinearSVC&#39;</span><span class="p">:</span>
        <span class="n">last_segment</span><span class="o">.</span><span class="n">RegressionModel</span><span class="o">.</span><span class="n">normalizationMethod</span> <span class="o">=</span> <span class="n">REGRESSION_NORMALIZATION_METHOD</span><span class="o">.</span><span class="n">SIMPLEMAX</span><span class="o">.</span><span class="n">value</span>
    <span class="n">segmentation</span><span class="o">.</span><span class="n">add_Segment</span><span class="p">(</span><span class="n">last_segment</span><span class="p">)</span>
    <span class="n">mining_model</span><span class="o">.</span><span class="n">set_Segmentation</span><span class="p">(</span><span class="n">segmentation</span><span class="p">)</span>
    <span class="k">return</span> <span class="p">[</span><span class="n">mining_model</span><span class="p">]</span></div>


<div class="viewcode-block" id="get_reg_tab_for_reg_mining_model"><span class="k">def</span> <span class="nf">get_reg_tab_for_reg_mining_model</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">index</span><span class="p">,</span> <span class="n">categorical_values</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Generates Regression Table for multi-class linear models</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        An instance of Scikit-learn model.</span>
<span class="sd">    col_names : List</span>
<span class="sd">        Contains list of feature/column names.</span>
<span class="sd">    index : int</span>
<span class="sd">    categoric_values : tuple</span>
<span class="sd">        Contains Categorical attribute names and its values</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    Returns Nyoka&#39;s RegressionTable object</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">reg_tab</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">RegressionTable</span><span class="p">(</span><span class="n">intercept</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">{:.16f}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">intercept_</span><span class="p">[</span><span class="n">index</span><span class="p">]))</span>
    <span class="k">for</span> <span class="n">idx</span><span class="p">,</span> <span class="n">coef</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">coef_</span><span class="p">[</span><span class="n">index</span><span class="p">]):</span>
        <span class="n">reg_tab</span><span class="o">.</span><span class="n">add_NumericPredictor</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">NumericPredictor</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="n">col_names</span><span class="p">[</span><span class="n">idx</span><span class="p">],</span><span class="n">coefficient</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">{:.16f}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">coef</span><span class="p">)))</span>
    <span class="k">return</span> <span class="p">[</span><span class="n">reg_tab</span><span class="p">]</span></div>


<div class="viewcode-block" id="get_anomalydetection_model"><span class="k">def</span> <span class="nf">get_anomalydetection_model</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">mining_imp_val</span><span class="p">,</span> <span class="n">categoric_values</span><span class="p">,</span> <span class="n">model_name</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Creates xml elements for anomaly detction models</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        An instance of Scikit-learn model.</span>
<span class="sd">    derived_col_names : List</span>
<span class="sd">        Contains column names after preprocessing</span>
<span class="sd">    col_names : List</span>
<span class="sd">        Contains list of feature/column names.</span>
<span class="sd">    target_name : String</span>
<span class="sd">        Name of the Target column.</span>
<span class="sd">    mining_imp_val : tuple</span>
<span class="sd">        Contains the mining_attributes,mining_strategy, mining_impute_value</span>
<span class="sd">    categoric_values : tuple</span>
<span class="sd">        Contains Categorical attribute names and its values</span>
<span class="sd">    model_name : string</span>
<span class="sd">        Name of the model</span>


<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    anomaly_detection_model : List</span>
<span class="sd">        Returns Nyoka&#39;s AnomalyDetectionModel object</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">anomaly_detection_model</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
    <span class="k">if</span> <span class="s1">&#39;OneClassSVM&#39;</span> <span class="ow">in</span> <span class="nb">str</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="p">):</span>
        <span class="n">svm_model</span> <span class="o">=</span> <span class="n">get_supportVectorMachine_models</span><span class="p">(</span><span class="n">model</span><span class="p">,</span>
                                                    <span class="n">derived_col_names</span><span class="p">,</span>
                                                    <span class="n">col_names</span><span class="p">,</span>
                                                    <span class="n">target_name</span><span class="p">,</span>
                                                    <span class="n">mining_imp_val</span><span class="p">,</span>
                                                    <span class="n">categoric_values</span><span class="p">,</span> <span class="n">model_name</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">anomaly_detection_model</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
            <span class="n">pml</span><span class="o">.</span><span class="n">AnomalyDetectionModel</span><span class="p">(</span>
                <span class="n">modelName</span><span class="o">=</span><span class="n">model_name</span> <span class="k">if</span> <span class="n">model_name</span> <span class="k">else</span> <span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
                <span class="n">algorithmType</span><span class="o">=</span><span class="n">ANOMALY_DETECTION_ALGORITHM</span><span class="o">.</span><span class="n">ONE_CLASS_SVM</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                <span class="n">functionName</span><span class="o">=</span><span class="n">MINING_FUNCTION</span><span class="o">.</span><span class="n">REGRESSION</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                <span class="n">MiningSchema</span><span class="o">=</span><span class="n">get_mining_schema</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">mining_imp_val</span><span class="p">,</span><span class="n">categoric_values</span><span class="p">),</span>
                <span class="n">Output</span><span class="o">=</span><span class="n">get_anomaly_detection_output</span><span class="p">(</span><span class="n">model</span><span class="p">),</span>
                <span class="n">SupportVectorMachineModel</span><span class="o">=</span><span class="n">svm_model</span>
            <span class="p">)</span>
        <span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">mining_schema</span> <span class="o">=</span> <span class="n">get_mining_schema</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">mining_imp_val</span><span class="p">,</span><span class="n">categoric_values</span><span class="p">)</span>
        <span class="n">ensemble_model</span> <span class="o">=</span> <span class="n">get_ensemble_models</span><span class="p">(</span><span class="n">model</span><span class="p">,</span>
                                            <span class="n">derived_col_names</span><span class="p">,</span>
                                            <span class="n">col_names</span><span class="p">,</span>
                                            <span class="s1">&#39;avg_path_length&#39;</span><span class="p">,</span>
                                            <span class="n">mining_imp_val</span><span class="p">,</span>
                                            <span class="n">categoric_values</span><span class="p">,</span> <span class="n">model_name</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">anomaly_detection_model</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
            <span class="n">pml</span><span class="o">.</span><span class="n">AnomalyDetectionModel</span><span class="p">(</span>
                <span class="n">modelName</span><span class="o">=</span><span class="n">model_name</span> <span class="k">if</span> <span class="n">model_name</span> <span class="k">else</span> <span class="s2">&quot;IsolationForest&quot;</span><span class="p">,</span>
                <span class="n">algorithmType</span><span class="o">=</span><span class="n">ANOMALY_DETECTION_ALGORITHM</span><span class="o">.</span><span class="n">ISOLATION_FOREST</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                <span class="n">functionName</span><span class="o">=</span><span class="n">MINING_FUNCTION</span><span class="o">.</span><span class="n">REGRESSION</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                <span class="n">MiningSchema</span><span class="o">=</span><span class="n">mining_schema</span><span class="p">,</span>
                <span class="n">Output</span><span class="o">=</span><span class="n">get_anomaly_detection_output</span><span class="p">(</span><span class="n">model</span><span class="p">),</span>
                <span class="n">sampleDataSize</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">max_samples_</span><span class="p">),</span>
                <span class="n">MiningModel</span><span class="o">=</span><span class="n">ensemble_model</span>
            <span class="p">)</span>
    <span class="p">)</span>
    <span class="k">return</span> <span class="n">anomaly_detection_model</span></div>


<div class="viewcode-block" id="get_anomaly_detection_output"><span class="k">def</span> <span class="nf">get_anomaly_detection_output</span><span class="p">(</span><span class="n">model</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Generates output for anomaly detection models</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        Scikit-learn&#39;s model object</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    output_fields :</span>
<span class="sd">        Returns Nyoka&#39;s Output object</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">output_fields</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
    <span class="n">output_fields</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">OutputField</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">&quot;anomalyScore&quot;</span><span class="p">,</span> 
                                            <span class="n">optype</span><span class="o">=</span><span class="n">OPTYPE</span><span class="o">.</span><span class="n">CONTINUOUS</span><span class="o">.</span><span class="n">value</span><span class="p">,</span> 
                                            <span class="n">dataType</span><span class="o">=</span><span class="n">DATATYPE</span><span class="o">.</span><span class="n">DOUBLE</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                                            <span class="n">feature</span><span class="o">=</span><span class="n">RESULT_FEATURE</span><span class="o">.</span><span class="n">PREDICTED_VALUE</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                                            <span class="n">isFinalResult</span><span class="o">=</span><span class="s2">&quot;false&quot;</span><span class="p">))</span>
    <span class="n">thresh</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">thresh</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">threshold_</span>
    <span class="k">except</span><span class="p">:</span>
        <span class="n">thresh</span> <span class="o">=</span> <span class="mi">0</span>
    
    <span class="n">offset</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="n">operator</span> <span class="o">=</span> <span class="n">SIMPLE_PREDICATE_OPERATOR</span><span class="o">.</span><span class="n">LESS_THAN</span><span class="o">.</span><span class="n">value</span>
    <span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">==</span> <span class="s2">&quot;IsolationForest&quot;</span><span class="p">:</span>
        <span class="n">operator</span> <span class="o">=</span> <span class="n">SIMPLE_PREDICATE_OPERATOR</span><span class="o">.</span><span class="n">GREATER_THAN</span><span class="o">.</span><span class="n">value</span>
        <span class="n">offset</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">offset_</span>
    <span class="n">thresh</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span> <span class="o">*</span> <span class="p">(</span><span class="n">thresh</span> <span class="o">+</span> <span class="n">offset</span><span class="p">)</span>

    <span class="n">output_fields</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
        <span class="n">pml</span><span class="o">.</span><span class="n">OutputField</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">&quot;outlier&quot;</span><span class="p">,</span>
                        <span class="n">optype</span><span class="o">=</span><span class="n">OPTYPE</span><span class="o">.</span><span class="n">CATEGORICAL</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                        <span class="n">dataType</span><span class="o">=</span><span class="n">DATATYPE</span><span class="o">.</span><span class="n">BOOLEAN</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                        <span class="n">feature</span><span class="o">=</span><span class="n">RESULT_FEATURE</span><span class="o">.</span><span class="n">DECISION</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                        <span class="n">isFinalResult</span><span class="o">=</span><span class="s2">&quot;true&quot;</span><span class="p">,</span> 
                        <span class="n">Apply</span><span class="o">=</span><span class="n">pml</span><span class="o">.</span><span class="n">Apply</span><span class="p">(</span><span class="n">function</span><span class="o">=</span><span class="n">operator</span><span class="p">,</span> 
                                        <span class="n">FieldRef</span><span class="o">=</span><span class="p">[</span><span class="n">pml</span><span class="o">.</span><span class="n">FieldRef</span><span class="p">(</span><span class="n">field</span><span class="o">=</span><span class="s2">&quot;anomalyScore&quot;</span><span class="p">)],</span>
                                        <span class="n">Constant</span><span class="o">=</span><span class="p">[</span><span class="n">pml</span><span class="o">.</span><span class="n">Constant</span><span class="p">(</span><span class="n">dataType</span><span class="o">=</span><span class="n">DATATYPE</span><span class="o">.</span><span class="n">DOUBLE</span><span class="o">.</span><span class="n">value</span><span class="p">,</span> 
                                        <span class="n">valueOf_</span><span class="o">=</span><span class="s2">&quot;0&quot;</span> <span class="k">if</span> <span class="n">thresh</span><span class="o">==</span><span class="mi">0</span> <span class="k">else</span> <span class="s2">&quot;</span><span class="si">{:.16f}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">thresh</span><span class="p">))]))</span>
    <span class="p">)</span>
    <span class="k">return</span> <span class="n">pml</span><span class="o">.</span><span class="n">Output</span><span class="p">(</span><span class="n">OutputField</span><span class="o">=</span><span class="n">output_fields</span><span class="p">)</span></div>


<div class="viewcode-block" id="get_clustering_model"><span class="k">def</span> <span class="nf">get_clustering_model</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">mining_imp_val</span><span class="p">,</span><span class="n">categoric_values</span><span class="p">,</span><span class="n">model_name</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Generates PMML elements for clustering models</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        An instance of Scikit-learn model.</span>
<span class="sd">    derived_col_names : List</span>
<span class="sd">        Contains column names after preprocessing</span>
<span class="sd">    col_names : List</span>
<span class="sd">        Contains list of feature/column names.</span>
<span class="sd">    target_name : String</span>
<span class="sd">        Name of the Target column.</span>
<span class="sd">    mining_imp_val : tuple</span>
<span class="sd">        Contains the mining_attributes,mining_strategy, mining_impute_value</span>
<span class="sd">    categoric_values : tuple</span>
<span class="sd">        Contains Categorical attribute names and its values</span>
<span class="sd">    model_name : string</span>
<span class="sd">        Name of the model</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    clustering_models : List</span>
<span class="sd">        Returns Nyoka&#39;s ClusteringModel object</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
    <span class="n">clustering_models</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
    <span class="n">model_kwargs</span> <span class="o">=</span> <span class="n">get_model_kwargs</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">mining_imp_val</span><span class="p">,</span><span class="n">categoric_values</span><span class="p">)</span>
    <span class="n">values</span><span class="p">,</span> <span class="n">counts</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">unique</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">labels_</span><span class="p">,</span><span class="n">return_counts</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
    <span class="n">model_kwargs</span><span class="p">[</span><span class="s2">&quot;Output&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">get_output_for_clustering</span><span class="p">(</span><span class="n">values</span><span class="p">)</span>
    <span class="n">clustering_models</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
        <span class="n">pml</span><span class="o">.</span><span class="n">ClusteringModel</span><span class="p">(</span>
            <span class="n">modelClass</span><span class="o">=</span><span class="n">CLUSTERING_MODEL_CLASS</span><span class="o">.</span><span class="n">CENTER_BASED</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
            <span class="n">modelName</span><span class="o">=</span><span class="n">model_name</span> <span class="k">if</span> <span class="n">model_name</span> <span class="k">else</span> <span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
            <span class="n">numberOfClusters</span><span class="o">=</span><span class="n">get_cluster_num</span><span class="p">(</span><span class="n">model</span><span class="p">),</span>
            <span class="n">ComparisonMeasure</span><span class="o">=</span><span class="n">get_comp_measure</span><span class="p">(),</span>
            <span class="n">ClusteringField</span><span class="o">=</span><span class="n">get_clustering_flds</span><span class="p">(</span><span class="n">derived_col_names</span><span class="p">),</span>
            <span class="n">Cluster</span><span class="o">=</span><span class="n">get_cluster_vals</span><span class="p">(</span><span class="n">model</span><span class="p">,</span><span class="n">counts</span><span class="p">),</span>
            <span class="o">**</span><span class="n">model_kwargs</span>

        <span class="p">)</span>
    <span class="p">)</span>

    <span class="k">return</span> <span class="n">clustering_models</span></div>


<div class="viewcode-block" id="get_output_for_clustering"><span class="k">def</span> <span class="nf">get_output_for_clustering</span><span class="p">(</span><span class="n">values</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Generates output for clustering models</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        An instance of Scikit-learn model.</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    output_fields : List</span>
<span class="sd">        Returns Nyoka&#39;s Output object</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">output_fields</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
    <span class="k">for</span> <span class="n">idx</span><span class="p">,</span> <span class="n">val</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">values</span><span class="p">):</span>
        <span class="n">output_fields</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
            <span class="n">pml</span><span class="o">.</span><span class="n">OutputField</span><span class="p">(</span>
                <span class="n">name</span><span class="o">=</span><span class="s2">&quot;affinity(&quot;</span><span class="o">+</span><span class="nb">str</span><span class="p">(</span><span class="n">idx</span><span class="p">)</span><span class="o">+</span><span class="s2">&quot;)&quot;</span><span class="p">,</span>
                <span class="n">optype</span><span class="o">=</span><span class="n">OPTYPE</span><span class="o">.</span><span class="n">CONTINUOUS</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                <span class="n">dataType</span><span class="o">=</span><span class="n">DATATYPE</span><span class="o">.</span><span class="n">DOUBLE</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                <span class="n">feature</span><span class="o">=</span><span class="n">RESULT_FEATURE</span><span class="o">.</span><span class="n">ENTITY_AFFINITY</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                <span class="n">value</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">val</span><span class="p">)</span>
            <span class="p">)</span>
        <span class="p">)</span>
    <span class="n">output_fields</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">OutputField</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">&quot;cluster&quot;</span><span class="p">,</span> <span class="n">optype</span><span class="o">=</span><span class="n">OPTYPE</span><span class="o">.</span><span class="n">CATEGORICAL</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>\
        <span class="n">dataType</span><span class="o">=</span><span class="n">DATATYPE</span><span class="o">.</span><span class="n">STRING</span><span class="o">.</span><span class="n">value</span><span class="p">,</span><span class="n">feature</span><span class="o">=</span><span class="n">RESULT_FEATURE</span><span class="o">.</span><span class="n">PREDICTED_VALUE</span><span class="o">.</span><span class="n">value</span><span class="p">))</span>
    <span class="k">return</span> <span class="n">pml</span><span class="o">.</span><span class="n">Output</span><span class="p">(</span><span class="n">OutputField</span><span class="o">=</span><span class="n">output_fields</span><span class="p">)</span></div>
        


<div class="viewcode-block" id="get_cluster_vals"><span class="k">def</span> <span class="nf">get_cluster_vals</span><span class="p">(</span><span class="n">model</span><span class="p">,</span><span class="n">counts</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Generates cluster information for clustering models</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        An instance of Scikit-learn model.</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    cluster_flds : List</span>
<span class="sd">        Returns Nyoka&#39;s Cluster object</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">centroids</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">cluster_centers_</span>
    <span class="n">cluster_flds</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">centroid_idx</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">centroids</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]):</span>
        <span class="n">centroid_values</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>
        <span class="n">centroid_flds</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">ArrayType</span><span class="p">(</span><span class="n">type_</span><span class="o">=</span><span class="n">ARRAY_TYPE</span><span class="o">.</span><span class="n">REAL</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">centroid_cordinate_idx</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">centroids</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]):</span>
            <span class="n">centroid_flds</span><span class="o">.</span><span class="n">content_</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">centroid_values</span> <span class="o">+</span> <span class="s2">&quot;</span><span class="si">{:.16f}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">centroids</span><span class="p">[</span><span class="n">centroid_idx</span><span class="p">][</span><span class="n">centroid_cordinate_idx</span><span class="p">])</span>
            <span class="n">centroid_values</span> <span class="o">=</span> <span class="n">centroid_flds</span><span class="o">.</span><span class="n">content_</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">value</span> <span class="o">+</span> <span class="s2">&quot; &quot;</span>
        <span class="n">cluster_flds</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">Cluster</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">centroid_idx</span><span class="p">),</span> <span class="n">Array</span><span class="o">=</span><span class="n">centroid_flds</span><span class="p">,</span><span class="n">size</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">counts</span><span class="p">[</span><span class="n">centroid_idx</span><span class="p">])))</span>
    <span class="k">return</span> <span class="n">cluster_flds</span></div>


<div class="viewcode-block" id="get_cluster_num"><span class="k">def</span> <span class="nf">get_cluster_num</span><span class="p">(</span><span class="n">model</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Returns number of cluster for clustering models</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        An instance of Scikit-learn model.</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>

<span class="sd">    model.n_clusters: Integer</span>

<span class="sd">        Returns the number of clusters</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">model</span><span class="o">.</span><span class="n">n_clusters</span></div>


<div class="viewcode-block" id="get_comp_measure"><span class="k">def</span> <span class="nf">get_comp_measure</span><span class="p">():</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Generates comparison measure information for clustering models</span>
<span class="sd">    </span>
<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    Returns Nyoka&#39;s ComparisonMeasure object</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">comp_equation</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">euclidean</span><span class="p">()</span>
    <span class="k">return</span> <span class="n">pml</span><span class="o">.</span><span class="n">ComparisonMeasure</span><span class="p">(</span><span class="n">euclidean</span><span class="o">=</span><span class="n">comp_equation</span><span class="p">,</span> <span class="n">kind</span><span class="o">=</span><span class="n">COMPARISON_MEASURE_KIND</span><span class="o">.</span><span class="n">DISTANCE</span><span class="o">.</span><span class="n">value</span><span class="p">)</span></div>


<div class="viewcode-block" id="get_clustering_flds"><span class="k">def</span> <span class="nf">get_clustering_flds</span><span class="p">(</span><span class="n">col_names</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Generates cluster fields for clustering models</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    col_names :</span>
<span class="sd">        Contains list of feature/column names.</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    clustering_flds: List</span>
<span class="sd">        Returns Nyoka&#39;s ClusteringField object</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">clustering_flds</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">col_names</span><span class="p">:</span>
        <span class="n">clustering_flds</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">ClusteringField</span><span class="p">(</span><span class="n">field</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">name</span><span class="p">)))</span>
    <span class="k">return</span> <span class="n">clustering_flds</span></div>


<div class="viewcode-block" id="get_nearestNeighbour_model"><span class="k">def</span> <span class="nf">get_nearestNeighbour_model</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">mining_imp_val</span><span class="p">,</span><span class="n">categoric_values</span><span class="p">,</span><span class="n">model_name</span><span class="p">):</span>
    
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Generates PMML elements for nearest neighbour model</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        An instance of Scikit-learn model.</span>
<span class="sd">    derived_col_names : List</span>
<span class="sd">        Contains column names after preprocessing</span>
<span class="sd">    col_names : List</span>
<span class="sd">        Contains list of feature/column names.</span>
<span class="sd">    target_name : String</span>
<span class="sd">        Name of the Target column.</span>
<span class="sd">    mining_imp_val : tuple</span>
<span class="sd">        Contains the mining_attributes,mining_strategy, mining_impute_value</span>
<span class="sd">    categoric_values : tuple</span>
<span class="sd">        Contains Categorical attribute names and its values</span>
<span class="sd">    model_name : string</span>
<span class="sd">        Name of the model</span>
<span class="sd">    </span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    nearest_neighbour_model :</span>
<span class="sd">        Returns Nyoka&#39;s NearestNeighborModel object</span>
<span class="sd">        </span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">model_kwargs</span> <span class="o">=</span> <span class="n">get_model_kwargs</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">mining_imp_val</span><span class="p">,</span><span class="n">categoric_values</span><span class="p">)</span>
    <span class="n">nearest_neighbour_model</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
    <span class="n">nearest_neighbour_model</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
        <span class="n">pml</span><span class="o">.</span><span class="n">NearestNeighborModel</span><span class="p">(</span>
            <span class="n">modelName</span><span class="o">=</span><span class="n">model_name</span> <span class="k">if</span> <span class="n">model_name</span> <span class="k">else</span> <span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
            <span class="n">continuousScoringMethod</span><span class="o">=</span><span class="n">CONTINUOUS_SCORING_METHOD</span><span class="o">.</span><span class="n">AVERAGE</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
            <span class="n">algorithmName</span><span class="o">=</span><span class="s2">&quot;KNN&quot;</span><span class="p">,</span>
            <span class="n">numberOfNeighbors</span><span class="o">=</span><span class="n">model</span><span class="o">.</span><span class="n">n_neighbors</span><span class="p">,</span>
            <span class="n">KNNInputs</span><span class="o">=</span><span class="n">get_knn_inputs</span><span class="p">(</span><span class="n">derived_col_names</span><span class="p">),</span>
            <span class="n">ComparisonMeasure</span><span class="o">=</span><span class="n">get_comparison_measure</span><span class="p">(</span><span class="n">model</span><span class="p">),</span>
            <span class="n">TrainingInstances</span><span class="o">=</span><span class="n">get_training_instances</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">),</span>
            <span class="o">**</span><span class="n">model_kwargs</span>
        <span class="p">)</span>
    <span class="p">)</span>
    <span class="k">return</span> <span class="n">nearest_neighbour_model</span></div>


<div class="viewcode-block" id="get_training_instances"><span class="k">def</span> <span class="nf">get_training_instances</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It returns the Training Instance element.</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        An instance of Scikit-learn model.</span>
<span class="sd">    derived_col_names : List</span>
<span class="sd">        Contains column names after preprocessing        </span>
<span class="sd">    target_name : String</span>
<span class="sd">        Name of the Target column.</span>
<span class="sd">    </span>
<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    TrainingInstances :</span>
<span class="sd">        Returns Nyoka&#39;s TrainingInstances object</span>
<span class="sd">        </span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">pml</span><span class="o">.</span><span class="n">TrainingInstances</span><span class="p">(</span>
        <span class="n">InstanceFields</span><span class="o">=</span><span class="n">get_instance_fields</span><span class="p">(</span><span class="n">derived_col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">),</span>
        <span class="n">InlineTable</span><span class="o">=</span><span class="n">get_inline_table</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>
    <span class="p">)</span></div>


<div class="viewcode-block" id="get_inline_table"><span class="k">def</span> <span class="nf">get_inline_table</span><span class="p">(</span><span class="n">model</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It Returns the Inline Table element of the model.</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        An instance of Scikit-learn model.</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    InlineTable :</span>
<span class="sd">        Returns Nyoka&#39;s InlineTable object</span>
<span class="sd">        </span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">rows</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">x</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">_tree</span><span class="o">.</span><span class="n">get_arrays</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
    <span class="n">y</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">_y</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>

    <span class="n">X</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">idx</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_tree</span><span class="o">.</span><span class="n">get_arrays</span><span class="p">()[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">])):</span>
        <span class="n">X</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;x&quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">idx</span> <span class="o">+</span> <span class="mi">1</span><span class="p">))</span>

    <span class="k">for</span> <span class="n">idx</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">x</span><span class="p">)):</span>
        <span class="n">row</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">row</span><span class="p">()</span>
        <span class="n">row</span><span class="o">.</span><span class="n">elementobjs_</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;y&#39;</span><span class="p">]</span> <span class="o">+</span> <span class="n">X</span>
        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="s1">&#39;classes_&#39;</span><span class="p">):</span>
            <span class="n">row</span><span class="o">.</span><span class="n">y</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">classes_</span><span class="p">[</span><span class="n">y</span><span class="p">[</span><span class="n">idx</span><span class="p">]]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">row</span><span class="o">.</span><span class="n">y</span> <span class="o">=</span> <span class="n">y</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span>
        <span class="k">for</span> <span class="n">idx_2</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="n">idx</span><span class="p">])):</span>
            <span class="n">exec</span><span class="p">(</span><span class="s2">&quot;row.&quot;</span> <span class="o">+</span> <span class="n">X</span><span class="p">[</span><span class="n">idx_2</span><span class="p">]</span> <span class="o">+</span> <span class="s2">&quot;=&quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="n">idx</span><span class="p">][</span><span class="n">idx_2</span><span class="p">]))</span>
        <span class="n">rows</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">row</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">pml</span><span class="o">.</span><span class="n">InlineTable</span><span class="p">(</span><span class="n">row</span><span class="o">=</span><span class="n">rows</span><span class="p">)</span></div>


<div class="viewcode-block" id="get_instance_fields"><span class="k">def</span> <span class="nf">get_instance_fields</span><span class="p">(</span><span class="n">derived_col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It returns the Instance field element.</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>

<span class="sd">    derived_col_names : List</span>
<span class="sd">        Contains column names after preprocessing.        </span>
<span class="sd">    target_name : String</span>
<span class="sd">        Name of the Target column.</span>
<span class="sd">    </span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    InstanceFields :</span>
<span class="sd">        Returns Nyoka&#39;s InstanceFields object</span>
<span class="sd">        </span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">instance_fields</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
    <span class="n">instance_fields</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">InstanceField</span><span class="p">(</span><span class="n">field</span><span class="o">=</span><span class="n">target_name</span><span class="p">,</span> <span class="n">column</span><span class="o">=</span><span class="s2">&quot;y&quot;</span><span class="p">))</span>
    <span class="k">for</span> <span class="p">(</span><span class="n">index</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">derived_col_names</span><span class="p">):</span>
        <span class="n">instance_fields</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">InstanceField</span><span class="p">(</span><span class="n">field</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">name</span><span class="p">),</span> <span class="n">column</span><span class="o">=</span><span class="s2">&quot;x&quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">index</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)))</span>
    <span class="k">return</span> <span class="n">pml</span><span class="o">.</span><span class="n">InstanceFields</span><span class="p">(</span><span class="n">InstanceField</span><span class="o">=</span><span class="n">instance_fields</span><span class="p">)</span></div>


<div class="viewcode-block" id="get_comparison_measure"><span class="k">def</span> <span class="nf">get_comparison_measure</span><span class="p">(</span><span class="n">model</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It return the Comparison measure element for nearest neighbour model.</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        An instance of Scikit-learn model.</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    comp_measure :</span>
<span class="sd">        Returns Nyoka&#39;s ComparisonMeasure object.</span>
<span class="sd">        </span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="n">effective_metric_</span> <span class="o">==</span> <span class="s1">&#39;euclidean&#39;</span><span class="p">:</span>
        <span class="n">comp_measure</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">ComparisonMeasure</span><span class="p">(</span><span class="n">euclidean</span><span class="o">=</span><span class="n">pml</span><span class="o">.</span><span class="n">euclidean</span><span class="p">(),</span> <span class="n">kind</span><span class="o">=</span><span class="n">COMPARISON_MEASURE_KIND</span><span class="o">.</span><span class="n">DISTANCE</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">model</span><span class="o">.</span><span class="n">effective_metric_</span> <span class="o">==</span> <span class="s1">&#39;minkowski&#39;</span><span class="p">:</span>
        <span class="n">comp_measure</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">ComparisonMeasure</span><span class="p">(</span><span class="n">minkowski</span><span class="o">=</span><span class="n">pml</span><span class="o">.</span><span class="n">minkowski</span><span class="p">(</span><span class="n">p_parameter</span><span class="o">=</span><span class="n">model</span><span class="o">.</span><span class="n">p</span><span class="p">),</span> <span class="n">kind</span><span class="o">=</span><span class="n">COMPARISON_MEASURE_KIND</span><span class="o">.</span><span class="n">DISTANCE</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">model</span><span class="o">.</span><span class="n">effective_metric_</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;manhattan&#39;</span><span class="p">,</span><span class="s1">&#39;cityblock&#39;</span><span class="p">]:</span>
        <span class="n">comp_measure</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">ComparisonMeasure</span><span class="p">(</span><span class="n">cityBlock</span><span class="o">=</span><span class="n">pml</span><span class="o">.</span><span class="n">cityBlock</span><span class="p">(),</span> <span class="n">kind</span><span class="o">=</span><span class="n">COMPARISON_MEASURE_KIND</span><span class="o">.</span><span class="n">DISTANCE</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">model</span><span class="o">.</span><span class="n">effective_metric_</span> <span class="o">==</span> <span class="s1">&#39;sqeuclidean&#39;</span><span class="p">:</span>
        <span class="n">comp_measure</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">ComparisonMeasure</span><span class="p">(</span><span class="n">squaredEuclidean</span><span class="o">=</span><span class="n">pml</span><span class="o">.</span><span class="n">squaredEuclidean</span><span class="p">(),</span> <span class="n">kind</span><span class="o">=</span><span class="n">COMPARISON_MEASURE_KIND</span><span class="o">.</span><span class="n">DISTANCE</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">model</span><span class="o">.</span><span class="n">effective_metric_</span> <span class="o">==</span> <span class="s1">&#39;chebyshev&#39;</span><span class="p">:</span>
        <span class="n">comp_measure</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">ComparisonMeasure</span><span class="p">(</span><span class="n">chebychev</span><span class="o">=</span><span class="n">pml</span><span class="o">.</span><span class="n">chebychev</span><span class="p">(),</span> <span class="n">kind</span><span class="o">=</span><span class="n">COMPARISON_MEASURE_KIND</span><span class="o">.</span><span class="n">DISTANCE</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">model</span><span class="o">.</span><span class="n">effective_metric_</span> <span class="o">==</span> <span class="s1">&#39;matching&#39;</span><span class="p">:</span>
        <span class="n">comp_measure</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">ComparisonMeasure</span><span class="p">(</span><span class="n">simpleMatching</span><span class="o">=</span><span class="n">pml</span><span class="o">.</span><span class="n">simpleMatching</span><span class="p">(),</span> <span class="n">kind</span><span class="o">=</span><span class="n">COMPARISON_MEASURE_KIND</span><span class="o">.</span><span class="n">SIMILARITY</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">model</span><span class="o">.</span><span class="n">effective_metric_</span> <span class="o">==</span> <span class="s1">&#39;jaccard&#39;</span><span class="p">:</span>
        <span class="n">comp_measure</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">ComparisonMeasure</span><span class="p">(</span><span class="n">jaccard</span><span class="o">=</span><span class="n">pml</span><span class="o">.</span><span class="n">jaccard</span><span class="p">(),</span> <span class="n">kind</span><span class="o">=</span><span class="n">COMPARISON_MEASURE_KIND</span><span class="o">.</span><span class="n">SIMILARITY</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">model</span><span class="o">.</span><span class="n">effective_metric_</span> <span class="o">==</span> <span class="s1">&#39;rogerstanimoto&#39;</span><span class="p">:</span>
        <span class="n">comp_measure</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">ComparisonMeasure</span><span class="p">(</span><span class="n">tanimoto</span><span class="o">=</span><span class="n">pml</span><span class="o">.</span><span class="n">tanimoto</span><span class="p">(),</span> <span class="n">kind</span><span class="o">=</span><span class="n">COMPARISON_MEASURE_KIND</span><span class="o">.</span><span class="n">SIMILARITY</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">{}</span><span class="s2"> metric is not implemented for KNN Model!&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">effective_metric_</span><span class="p">))</span>
    <span class="k">return</span> <span class="n">comp_measure</span></div>


<div class="viewcode-block" id="get_knn_inputs"><span class="k">def</span> <span class="nf">get_knn_inputs</span><span class="p">(</span><span class="n">col_names</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It returns the KNN Inputs element.</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    col_names : List</span>
<span class="sd">        Contains list of feature/column names.</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    KNNInputs :</span>
<span class="sd">        Returns Nyoka&#39;s KNNInputs object.</span>
<span class="sd">        </span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">knnInput</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
    <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">col_names</span><span class="p">:</span>
        <span class="n">knnInput</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">KNNInput</span><span class="p">(</span><span class="n">field</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">name</span><span class="p">)))</span>
    <span class="k">return</span> <span class="n">pml</span><span class="o">.</span><span class="n">KNNInputs</span><span class="p">(</span><span class="n">KNNInput</span><span class="o">=</span><span class="n">knnInput</span><span class="p">)</span></div>


<div class="viewcode-block" id="get_naiveBayesModel"><span class="k">def</span> <span class="nf">get_naiveBayesModel</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">mining_imp_val</span><span class="p">,</span><span class="n">categoric_values</span><span class="p">,</span><span class="n">model_name</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Generates PMML elements for naive bayes models</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        An instance of Scikit-learn model.</span>
<span class="sd">    derived_col_names : List</span>
<span class="sd">        Contains column names after preprocessing.</span>
<span class="sd">    col_names : List</span>
<span class="sd">        Contains list of feature/column names.</span>
<span class="sd">    target_name : String</span>
<span class="sd">        Name of the Target column.</span>
<span class="sd">    mining_imp_val : tuple</span>
<span class="sd">        Contains the mining_attributes,mining_strategy, mining_impute_value</span>
<span class="sd">    categoric_values : tuple</span>
<span class="sd">        Contains Categorical attribute names and its values</span>
<span class="sd">    model_name : string</span>
<span class="sd">        Name of the model</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    naive_bayes_model : List</span>
<span class="sd">        Returns Nyoka&#39;s NaiveBayesModel</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">model_kwargs</span> <span class="o">=</span> <span class="n">get_model_kwargs</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">mining_imp_val</span><span class="p">,</span><span class="n">categoric_values</span><span class="p">)</span>
    <span class="n">naive_bayes_model</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
    <span class="n">naive_bayes_model</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">NaiveBayesModel</span><span class="p">(</span>
        <span class="n">modelName</span><span class="o">=</span><span class="n">model_name</span> <span class="k">if</span> <span class="n">model_name</span> <span class="k">else</span> <span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
        <span class="n">BayesInputs</span><span class="o">=</span><span class="n">get_bayes_inputs</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">),</span>
        <span class="n">BayesOutput</span><span class="o">=</span><span class="n">get_bayes_output</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">target_name</span><span class="p">),</span>
        <span class="n">threshold</span><span class="o">=</span><span class="n">get_threshold</span><span class="p">(),</span>
        <span class="o">**</span><span class="n">model_kwargs</span>
    <span class="p">))</span>
    <span class="k">return</span> <span class="n">naive_bayes_model</span></div>


<div class="viewcode-block" id="get_threshold"><span class="k">def</span> <span class="nf">get_threshold</span><span class="p">():</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It returns the Threshold value for Naive Bayes models.</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    Returns the Threshold value</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="s1">&#39;0.001&#39;</span></div>


<div class="viewcode-block" id="get_bayes_output"><span class="k">def</span> <span class="nf">get_bayes_output</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">target_name</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It returns the Bayes Output element of the model</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        An instance of Scikit-learn model.</span>
<span class="sd">    target_name : String</span>
<span class="sd">        Name of the Target column.    </span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    BayesOutput :</span>
<span class="sd">        Returns Nyoka&#39;s BayesOutput object</span>
<span class="sd">        </span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">class_counts</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">class_count_</span>
    <span class="n">target_val_counts</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">TargetValueCounts</span><span class="p">()</span>
    <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">count</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">classes_</span><span class="p">,</span> <span class="n">class_counts</span><span class="p">):</span>
        <span class="n">tr_val</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">TargetValueCount</span><span class="p">(</span><span class="n">value</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">name</span><span class="p">),</span> <span class="n">count</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">count</span><span class="p">))</span>
        <span class="n">target_val_counts</span><span class="o">.</span><span class="n">add_TargetValueCount</span><span class="p">(</span><span class="n">tr_val</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">pml</span><span class="o">.</span><span class="n">BayesOutput</span><span class="p">(</span>
        <span class="n">fieldName</span><span class="o">=</span><span class="n">target_name</span><span class="p">,</span>
        <span class="n">TargetValueCounts</span><span class="o">=</span><span class="n">target_val_counts</span>
    <span class="p">)</span></div>



<div class="viewcode-block" id="get_bayes_inputs"><span class="k">def</span> <span class="nf">get_bayes_inputs</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It returns the Bayes Input element of the naive bayes model .</span>
<span class="sd">    </span>
<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        An instance of Scikit-learn model.</span>
<span class="sd">    derived_col_names : List</span>
<span class="sd">        Contains column names after preprocessing.</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    bayes_inputs :</span>
<span class="sd">        Returns Nyoka&#39;s BayesInput object.</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">bayes_inputs</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">BayesInputs</span><span class="p">()</span>
    <span class="k">for</span> <span class="n">indx</span><span class="p">,</span> <span class="n">name</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">derived_col_names</span><span class="p">):</span>
        <span class="n">means</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">theta_</span><span class="p">[:,</span> <span class="n">indx</span><span class="p">]</span>
        <span class="n">variances</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">sigma_</span><span class="p">[:,</span> <span class="n">indx</span><span class="p">]</span>
        <span class="n">target_val_stats</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">TargetValueStats</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">idx</span><span class="p">,</span> <span class="n">val</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">classes_</span><span class="p">):</span>
            <span class="n">target_val</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">TargetValueStat</span><span class="p">(</span>
                <span class="n">val</span><span class="p">,</span> <span class="n">GaussianDistribution</span><span class="o">=</span><span class="n">pml</span><span class="o">.</span><span class="n">GaussianDistribution</span><span class="p">(</span>
                    <span class="n">mean</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">{:.16f}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">means</span><span class="p">[</span><span class="n">idx</span><span class="p">]),</span>
                    <span class="n">variance</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">{:.16f}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">variances</span><span class="p">[</span><span class="n">idx</span><span class="p">])))</span>
            <span class="n">target_val_stats</span><span class="o">.</span><span class="n">add_TargetValueStat</span><span class="p">(</span><span class="n">target_val</span><span class="p">)</span>
        <span class="n">bayes_inputs</span><span class="o">.</span><span class="n">add_BayesInput</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">BayesInput</span><span class="p">(</span><span class="n">fieldName</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">name</span><span class="p">),</span>
                                               <span class="n">TargetValueStats</span><span class="o">=</span><span class="n">target_val_stats</span><span class="p">))</span>
    <span class="k">return</span> <span class="n">bayes_inputs</span></div>


<div class="viewcode-block" id="get_supportVectorMachine_models"><span class="k">def</span> <span class="nf">get_supportVectorMachine_models</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_names</span><span class="p">,</span>
 									<span class="n">mining_imp_val</span><span class="p">,</span> <span class="n">categoric_values</span><span class="p">,</span><span class="n">model_name</span><span class="p">):</span>
    
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Generates PMML elements for support vector machine models</span>
<span class="sd">    </span>
<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        An instance of Scikit-learn model.</span>
<span class="sd">    derived_col_names : List</span>
<span class="sd">        Contains column names after preprocessing.</span>
<span class="sd">    col_names : List</span>
<span class="sd">        Contains list of feature/column names.        </span>
<span class="sd">    target_names : String</span>
<span class="sd">        Name of the Target column.</span>
<span class="sd">    mining_imp_val : tuple</span>
<span class="sd">        Contains the mining_attributes,mining_strategy, mining_impute_value</span>
<span class="sd">    categoric_values : tuple</span>
<span class="sd">        Contains Categorical attribute names and its values</span>
<span class="sd">    model_name : string</span>
<span class="sd">        Name of the model</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    supportVector_models : List</span>
<span class="sd">        Returns Nyoka&#39;s SupportVectorMachineModel object</span>
<span class="sd">        </span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">model_kwargs</span> <span class="o">=</span> <span class="n">get_model_kwargs</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_names</span><span class="p">,</span> <span class="n">mining_imp_val</span><span class="p">,</span><span class="n">categoric_values</span><span class="p">)</span>
    <span class="n">supportVector_models</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
    <span class="n">kernel_type</span> <span class="o">=</span> <span class="n">get_kernel_type</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>
    <span class="n">supportVector_models</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">SupportVectorMachineModel</span><span class="p">(</span>
        <span class="n">modelName</span><span class="o">=</span><span class="n">model_name</span> <span class="k">if</span> <span class="n">model_name</span> <span class="k">else</span> <span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
        <span class="n">classificationMethod</span><span class="o">=</span><span class="n">get_classificationMethod</span><span class="p">(</span><span class="n">model</span><span class="p">),</span>
        <span class="n">VectorDictionary</span><span class="o">=</span><span class="n">get_vectorDictionary</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span> <span class="n">categoric_values</span><span class="p">),</span>
        <span class="n">SupportVectorMachine</span><span class="o">=</span><span class="n">get_supportVectorMachine</span><span class="p">(</span><span class="n">model</span><span class="p">),</span>
        <span class="o">**</span><span class="n">kernel_type</span><span class="p">,</span>
        <span class="o">**</span><span class="n">model_kwargs</span>
    <span class="p">))</span>

    <span class="k">return</span> <span class="n">supportVector_models</span></div>


<div class="viewcode-block" id="get_ensemble_models"><span class="k">def</span> <span class="nf">get_ensemble_models</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">mining_imp_val</span><span class="p">,</span> <span class="n">categoric_values</span><span class="p">,</span><span class="n">model_name</span><span class="p">):</span>
    
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Generates PMML elemenets for ensemble models</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        An instance of Scikit-learn model.</span>
<span class="sd">    derived_col_names : List</span>
<span class="sd">        Contains column names after preprocessing.</span>
<span class="sd">    col_names : List</span>
<span class="sd">        Contains list of feature/column names.</span>
<span class="sd">    target_name : String</span>
<span class="sd">        Name of the Target column.</span>
<span class="sd">    mining_imp_val : tuple</span>
<span class="sd">        Contains the mining_attributes,mining_strategy, mining_impute_value.</span>
<span class="sd">    categoric_values : tuple</span>
<span class="sd">        Contains Categorical attribute names and its values</span>
<span class="sd">    model_name : string</span>
<span class="sd">        Name of the model</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    mining_models : List</span>
<span class="sd">        Returns Nyoka&#39;s MiningModel object</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">model_kwargs</span> <span class="o">=</span> <span class="n">get_model_kwargs</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">mining_imp_val</span><span class="p">,</span><span class="n">categoric_values</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;GradientBoostingRegressor&#39;</span><span class="p">:</span>
        <span class="n">model_kwargs</span><span class="p">[</span><span class="s1">&#39;Targets&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">get_targets</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">target_name</span><span class="p">)</span>
        
    <span class="n">mining_models</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
    <span class="n">mining_models</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">MiningModel</span><span class="p">(</span>
        <span class="n">modelName</span><span class="o">=</span><span class="n">model_name</span> <span class="k">if</span> <span class="n">model_name</span> <span class="k">else</span> <span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
        <span class="n">Segmentation</span><span class="o">=</span><span class="n">get_outer_segmentation</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span>
                                            <span class="n">mining_imp_val</span><span class="p">,</span> <span class="n">categoric_values</span><span class="p">,</span> <span class="n">model_name</span><span class="p">),</span>
        <span class="o">**</span><span class="n">model_kwargs</span>
    <span class="p">))</span>
    <span class="k">return</span> <span class="n">mining_models</span></div>


<div class="viewcode-block" id="get_targets"><span class="k">def</span> <span class="nf">get_targets</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">target_name</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It returns the Target element of the model.</span>
<span class="sd">    </span>
<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        A Scikit-learn model instance.</span>
<span class="sd">    target_name : String</span>
<span class="sd">        Name of the Target column.</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    targets :</span>
<span class="sd">        Returns Nyoka&#39;s Target object</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;GradientBoostingRegressor&#39;</span><span class="p">:</span>
        <span class="n">targets</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">Targets</span><span class="p">(</span>
            <span class="n">Target</span><span class="o">=</span><span class="p">[</span>
                <span class="n">pml</span><span class="o">.</span><span class="n">Target</span><span class="p">(</span>
                    <span class="n">field</span><span class="o">=</span><span class="n">target_name</span><span class="p">,</span>
                    <span class="n">rescaleConstant</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">{:.16f}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">init_</span><span class="o">.</span><span class="n">mean</span><span class="p">),</span>
                    <span class="n">rescaleFactor</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">{:.16f}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">learning_rate</span><span class="p">)</span>
                <span class="p">)</span>
            <span class="p">]</span>
        <span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">targets</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">Targets</span><span class="p">(</span>
            <span class="n">Target</span><span class="o">=</span><span class="p">[</span>
                <span class="n">pml</span><span class="o">.</span><span class="n">Target</span><span class="p">(</span>
                    <span class="n">field</span><span class="o">=</span><span class="n">target_name</span><span class="p">,</span>
                    <span class="n">rescaleConstant</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">{:.16f}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">base_score</span><span class="p">)</span>
                <span class="p">)</span>
            <span class="p">]</span>
        <span class="p">)</span>
    <span class="k">return</span> <span class="n">targets</span></div>


<div class="viewcode-block" id="get_multiple_model_method"><span class="k">def</span> <span class="nf">get_multiple_model_method</span><span class="p">(</span><span class="n">model</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It returns the type of multiple model method for MiningModels.</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        A Scikit-learn model instance</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    The multiple model method for a MiningModel.</span>
<span class="sd">        </span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;GradientBoostingClassifier&#39;</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">MULTIPLE_MODEL_METHOD</span><span class="o">.</span><span class="n">MODEL_CHAIN</span><span class="o">.</span><span class="n">value</span>
    <span class="k">elif</span> <span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;GradientBoostingRegressor&#39;</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">MULTIPLE_MODEL_METHOD</span><span class="o">.</span><span class="n">SUM</span><span class="o">.</span><span class="n">value</span>
    <span class="k">elif</span> <span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;RandomForestClassifier&#39;</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">MULTIPLE_MODEL_METHOD</span><span class="o">.</span><span class="n">MAJORITY_VOTE</span><span class="o">.</span><span class="n">value</span>
    <span class="k">elif</span> <span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;RandomForestRegressor&#39;</span><span class="p">,</span><span class="s1">&#39;IsolationForest&#39;</span><span class="p">]:</span>
        <span class="k">return</span> <span class="n">MULTIPLE_MODEL_METHOD</span><span class="o">.</span><span class="n">AVERAGE</span><span class="o">.</span><span class="n">value</span></div>


<div class="viewcode-block" id="get_outer_segmentation"><span class="k">def</span> <span class="nf">get_outer_segmentation</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">mining_imp_val</span><span class="p">,</span> <span class="n">categoric_values</span><span class="p">,</span><span class="n">model_name</span><span class="p">):</span>
    
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It returns the Segmentation element of a MiningModel.</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        A Scikit-learn model instance.</span>
<span class="sd">    derived_col_names : List</span>
<span class="sd">        Contains column names after preprocessing.</span>
<span class="sd">    col_names : List</span>
<span class="sd">        Contains list of feature/column names.            </span>
<span class="sd">    target_name : String</span>
<span class="sd">        Name of the Target column.</span>
<span class="sd">    mining_imp_val : tuple</span>
<span class="sd">        Contains the mining_attributes,mining_strategy, mining_impute_value</span>
<span class="sd">    categoric_values : tuple</span>
<span class="sd">        Contains Categorical attribute names and its values</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    segmentation :</span>
<span class="sd">        Nyoka&#39;s Segmentation object</span>
<span class="sd">        </span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">segmentation</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">Segmentation</span><span class="p">(</span>
        <span class="n">multipleModelMethod</span><span class="o">=</span><span class="n">get_multiple_model_method</span><span class="p">(</span><span class="n">model</span><span class="p">),</span>
        <span class="n">Segment</span><span class="o">=</span><span class="n">get_segments</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">mining_imp_val</span><span class="p">,</span> <span class="n">categoric_values</span><span class="p">,</span><span class="n">model_name</span><span class="p">)</span>
    <span class="p">)</span>
    <span class="k">return</span> <span class="n">segmentation</span></div>


<div class="viewcode-block" id="get_segments"><span class="k">def</span> <span class="nf">get_segments</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">mining_imp_val</span><span class="p">,</span> <span class="n">categoric_values</span><span class="p">,</span><span class="n">model_name</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It returns the Segment element of a Segmentation.</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        A Scikit-learn model instance.</span>
<span class="sd">    derived_col_names : List</span>
<span class="sd">        Contains column names after preprocessing.</span>
<span class="sd">    col_names : List</span>
<span class="sd">        Contains list of feature/column names.    </span>
<span class="sd">    target_name : String</span>
<span class="sd">        Name of the Target column.    </span>
<span class="sd">    mining_imp_val : tuple</span>
<span class="sd">          Contains the mining_attributes,mining_strategy, mining_impute_value</span>
<span class="sd">    categoric_values : tuple</span>
<span class="sd">        Contains Categorical attribute names and its values</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    segments :</span>
<span class="sd">        Nyoka&#39;s Segment object</span>
<span class="sd">        </span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">segments</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="k">if</span> <span class="s1">&#39;GradientBoostingClassifier&#39;</span> <span class="ow">in</span> <span class="nb">str</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="p">):</span>
        <span class="n">segments</span> <span class="o">=</span> <span class="n">get_segments_for_gbc</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span>
                                        <span class="n">mining_imp_val</span><span class="p">,</span> <span class="n">categoric_values</span><span class="p">,</span> <span class="n">model_name</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">segments</span> <span class="o">=</span> <span class="n">get_inner_segments</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">segments</span></div>


<div class="viewcode-block" id="get_segments_for_gbc"><span class="k">def</span> <span class="nf">get_segments_for_gbc</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">mining_imp_val</span><span class="p">,</span> <span class="n">categoric_values</span><span class="p">,</span> <span class="n">model_name</span><span class="p">):</span>
    
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It returns list of Segments element of a Segmentation.</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        A Scikit-learn model instance.</span>
<span class="sd">    derived_col_names : List</span>
<span class="sd">        Contains column names after preprocessing.</span>
<span class="sd">    col_names : List</span>
<span class="sd">        Contains list of feature/column names.    </span>
<span class="sd">    target_name : String</span>
<span class="sd">        Name of the Target column.</span>
<span class="sd">    mining_imp_val : tuple</span>
<span class="sd">        Contains the mining_attributes,mining_strategy, mining_impute_value</span>
<span class="sd">    categoric_values : tuple</span>
<span class="sd">        Contains Categorical attribute names and its values</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    segments : List</span>
<span class="sd">        Nyoka&#39;s Segment object</span>
<span class="sd">        </span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">segments</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
    <span class="n">out_field_names</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
    <span class="k">for</span> <span class="n">estm_idx</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">estimators_</span><span class="p">[</span><span class="mi">0</span><span class="p">])):</span>
        <span class="n">mining_fields_for_first</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">col_names</span><span class="p">:</span>
            <span class="n">mining_fields_for_first</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">MiningField</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="n">name</span><span class="p">))</span>

        <span class="n">miningschema_for_first</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">MiningSchema</span><span class="p">(</span><span class="n">MiningField</span><span class="o">=</span><span class="n">mining_fields_for_first</span><span class="p">)</span>
        <span class="n">output_fields</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
        <span class="n">output_fields</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
            <span class="n">pml</span><span class="o">.</span><span class="n">OutputField</span><span class="p">(</span>
                <span class="n">name</span><span class="o">=</span><span class="s1">&#39;decisionFunction(&#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">estm_idx</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;)&#39;</span><span class="p">,</span>
                <span class="n">feature</span><span class="o">=</span><span class="n">RESULT_FEATURE</span><span class="o">.</span><span class="n">PREDICTED_VALUE</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                <span class="n">dataType</span><span class="o">=</span><span class="n">DATATYPE</span><span class="o">.</span><span class="n">DOUBLE</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                <span class="n">isFinalResult</span><span class="o">=</span><span class="kc">False</span>
            <span class="p">)</span>
        <span class="p">)</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">classes_</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
            <span class="n">output_fields</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                <span class="n">pml</span><span class="o">.</span><span class="n">OutputField</span><span class="p">(</span>
                    <span class="n">name</span><span class="o">=</span><span class="s1">&#39;transformedDecisionFunction(0)&#39;</span><span class="p">,</span>
                    <span class="n">feature</span><span class="o">=</span><span class="n">RESULT_FEATURE</span><span class="o">.</span><span class="n">TRANSFORMED_VALUE</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                    <span class="n">dataType</span><span class="o">=</span><span class="n">DATATYPE</span><span class="o">.</span><span class="n">DOUBLE</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                    <span class="n">isFinalResult</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
                    <span class="n">Apply</span><span class="o">=</span><span class="n">pml</span><span class="o">.</span><span class="n">Apply</span><span class="p">(</span>
                        <span class="n">function</span><span class="o">=</span><span class="n">FUNCTION</span><span class="o">.</span><span class="n">ADDITION</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                        <span class="n">Constant</span><span class="o">=</span><span class="p">[</span><span class="n">pml</span><span class="o">.</span><span class="n">Constant</span><span class="p">(</span>
                            <span class="n">dataType</span><span class="o">=</span><span class="n">DATATYPE</span><span class="o">.</span><span class="n">DOUBLE</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                            <span class="n">valueOf_</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">{:.16f}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">init_</span><span class="o">.</span><span class="n">prior</span><span class="p">)</span>
                        <span class="p">)],</span>
                        <span class="n">Apply_member</span><span class="o">=</span><span class="p">[</span><span class="n">pml</span><span class="o">.</span><span class="n">Apply</span><span class="p">(</span>
                            <span class="n">function</span><span class="o">=</span><span class="n">FUNCTION</span><span class="o">.</span><span class="n">MULTIPLICATION</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                            <span class="n">Constant</span><span class="o">=</span><span class="p">[</span><span class="n">pml</span><span class="o">.</span><span class="n">Constant</span><span class="p">(</span>
                                <span class="n">dataType</span><span class="o">=</span><span class="n">DATATYPE</span><span class="o">.</span><span class="n">DOUBLE</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                                <span class="n">valueOf_</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">{:.16f}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">learning_rate</span><span class="p">)</span>
                            <span class="p">)],</span>
                            <span class="n">FieldRef</span><span class="o">=</span><span class="p">[</span><span class="n">pml</span><span class="o">.</span><span class="n">FieldRef</span><span class="p">(</span>
                                <span class="n">field</span><span class="o">=</span><span class="s2">&quot;decisionFunction(0)&quot;</span><span class="p">,</span>
                            <span class="p">)]</span>
                        <span class="p">)]</span>
                    <span class="p">)</span>
                <span class="p">)</span>
            <span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">output_fields</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                <span class="n">pml</span><span class="o">.</span><span class="n">OutputField</span><span class="p">(</span>
                    <span class="n">name</span><span class="o">=</span><span class="s1">&#39;transformedDecisionFunction(&#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">estm_idx</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;)&#39;</span><span class="p">,</span>
                    <span class="n">feature</span><span class="o">=</span><span class="n">RESULT_FEATURE</span><span class="o">.</span><span class="n">TRANSFORMED_VALUE</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                    <span class="n">dataType</span><span class="o">=</span><span class="n">DATATYPE</span><span class="o">.</span><span class="n">DOUBLE</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                    <span class="n">isFinalResult</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
                    <span class="n">Apply</span><span class="o">=</span><span class="n">pml</span><span class="o">.</span><span class="n">Apply</span><span class="p">(</span>
                        <span class="n">function</span><span class="o">=</span><span class="n">FUNCTION</span><span class="o">.</span><span class="n">ADDITION</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                        <span class="n">Constant</span><span class="o">=</span><span class="p">[</span><span class="n">pml</span><span class="o">.</span><span class="n">Constant</span><span class="p">(</span>
                            <span class="n">dataType</span><span class="o">=</span><span class="n">DATATYPE</span><span class="o">.</span><span class="n">DOUBLE</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                            <span class="n">valueOf_</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">{:.16f}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">init_</span><span class="o">.</span><span class="n">priors</span><span class="p">[</span><span class="n">estm_idx</span><span class="p">])</span>
                        <span class="p">)],</span>
                        <span class="n">Apply_member</span><span class="o">=</span><span class="p">[</span><span class="n">pml</span><span class="o">.</span><span class="n">Apply</span><span class="p">(</span>
                            <span class="n">function</span><span class="o">=</span><span class="n">FUNCTION</span><span class="o">.</span><span class="n">MULTIPLICATION</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                            <span class="n">Constant</span><span class="o">=</span><span class="p">[</span><span class="n">pml</span><span class="o">.</span><span class="n">Constant</span><span class="p">(</span>
                                <span class="n">dataType</span><span class="o">=</span><span class="n">DATATYPE</span><span class="o">.</span><span class="n">DOUBLE</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                                <span class="n">valueOf_</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">{:.16f}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">learning_rate</span><span class="p">)</span>
                            <span class="p">)],</span>
                            <span class="n">FieldRef</span><span class="o">=</span><span class="p">[</span><span class="n">pml</span><span class="o">.</span><span class="n">FieldRef</span><span class="p">(</span>
                                <span class="n">field</span><span class="o">=</span><span class="s2">&quot;decisionFunction(&quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">estm_idx</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot;)&quot;</span><span class="p">,</span>
                            <span class="p">)]</span>
                        <span class="p">)]</span>
                    <span class="p">)</span>
                <span class="p">)</span>
            <span class="p">)</span>

        <span class="n">out_field_names</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;transformedDecisionFunction(&#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">estm_idx</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;)&#39;</span><span class="p">)</span>
        <span class="n">segments</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
            <span class="n">pml</span><span class="o">.</span><span class="n">Segment</span><span class="p">(</span>
                <span class="n">True_</span><span class="o">=</span><span class="n">pml</span><span class="o">.</span><span class="n">True_</span><span class="p">(),</span>
                <span class="nb">id</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">estm_idx</span><span class="p">),</span>
                <span class="n">MiningModel</span><span class="o">=</span><span class="n">pml</span><span class="o">.</span><span class="n">MiningModel</span><span class="p">(</span>
                    <span class="n">functionName</span><span class="o">=</span><span class="n">MINING_FUNCTION</span><span class="o">.</span><span class="n">REGRESSION</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                    <span class="n">modelName</span><span class="o">=</span><span class="s2">&quot;MiningModel&quot;</span><span class="p">,</span>
                    <span class="n">MiningSchema</span><span class="o">=</span><span class="n">miningschema_for_first</span><span class="p">,</span>
                    <span class="n">Output</span><span class="o">=</span><span class="n">pml</span><span class="o">.</span><span class="n">Output</span><span class="p">(</span><span class="n">OutputField</span><span class="o">=</span><span class="n">output_fields</span><span class="p">),</span>
                    <span class="n">Segmentation</span><span class="o">=</span><span class="n">pml</span><span class="o">.</span><span class="n">Segmentation</span><span class="p">(</span>
                        <span class="n">multipleModelMethod</span><span class="o">=</span><span class="n">MULTIPLE_MODEL_METHOD</span><span class="o">.</span><span class="n">SUM</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                        <span class="n">Segment</span><span class="o">=</span><span class="n">get_inner_segments</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span>
                                                   <span class="n">col_names</span><span class="p">,</span> <span class="n">estm_idx</span><span class="p">)</span>
                    <span class="p">)</span>
                <span class="p">)</span>
            <span class="p">)</span>
        <span class="p">)</span>
    <span class="n">reg_model</span> <span class="o">=</span> <span class="n">get_regrs_models</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">out_field_names</span><span class="p">,</span><span class="n">out_field_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">mining_imp_val</span><span class="p">,</span> <span class="n">categoric_values</span><span class="p">,</span> <span class="n">model_name</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
    <span class="n">reg_model</span><span class="o">.</span><span class="n">Output</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">classes_</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
        <span class="n">reg_model</span><span class="o">.</span><span class="n">normalizationMethod</span><span class="o">=</span><span class="n">REGRESSION_NORMALIZATION_METHOD</span><span class="o">.</span><span class="n">LOGISTIC</span><span class="o">.</span><span class="n">value</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">reg_model</span><span class="o">.</span><span class="n">normalizationMethod</span><span class="o">=</span><span class="n">REGRESSION_NORMALIZATION_METHOD</span><span class="o">.</span><span class="n">SOFTMAX</span><span class="o">.</span><span class="n">value</span>
    <span class="n">segments</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
        <span class="n">pml</span><span class="o">.</span><span class="n">Segment</span><span class="p">(</span>
            <span class="nb">id</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">estimators_</span><span class="p">[</span><span class="mi">0</span><span class="p">])),</span>
            <span class="n">True_</span><span class="o">=</span><span class="n">pml</span><span class="o">.</span><span class="n">True_</span><span class="p">(),</span>
            <span class="n">RegressionModel</span><span class="o">=</span><span class="n">reg_model</span>
        <span class="p">)</span>
    <span class="p">)</span>
    <span class="k">return</span> <span class="n">segments</span></div>


<div class="viewcode-block" id="get_inner_segments"><span class="k">def</span> <span class="nf">get_inner_segments</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">index</span><span class="p">):</span>
    
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It returns the segments of a Segmentation.</span>
<span class="sd">    </span>
<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        A Scikit-learn model instance.</span>
<span class="sd">    derived_col_names : List</span>
<span class="sd">        Contains column names after preprocessing.</span>
<span class="sd">    col_names : List</span>
<span class="sd">        Contains list of feature/column names.</span>
<span class="sd">    index : Integer</span>
<span class="sd">        The index of the estimator for the model</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    segments : List</span>
<span class="sd">        Nyoka&#39;s Segment object</span>
<span class="sd">        </span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
    <span class="n">segments</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
    <span class="k">for</span> <span class="n">estm_idx</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">n_estimators</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">asanyarray</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">estimators_</span><span class="p">)</span><span class="o">.</span><span class="n">ndim</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">estm</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">estimators_</span><span class="p">[</span><span class="n">estm_idx</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">estm</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">estimators_</span><span class="p">[</span><span class="n">estm_idx</span><span class="p">][</span><span class="n">index</span><span class="p">]</span>
        <span class="n">tree_features</span> <span class="o">=</span> <span class="n">estm</span><span class="o">.</span><span class="n">tree_</span><span class="o">.</span><span class="n">feature</span>
        <span class="n">features_</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">feat</span> <span class="ow">in</span> <span class="n">tree_features</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">feat</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">2</span> <span class="ow">and</span> <span class="n">feat</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">features_</span><span class="p">:</span>
                <span class="n">features_</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">feat</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">features_</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">mining_fields</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
            <span class="k">for</span> <span class="n">feat</span> <span class="ow">in</span> <span class="n">col_names</span><span class="p">:</span>
                <span class="n">mining_fields</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">MiningField</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="n">feat</span><span class="p">))</span>
            <span class="n">segments</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                <span class="n">pml</span><span class="o">.</span><span class="n">Segment</span><span class="p">(</span>
                    <span class="n">True_</span><span class="o">=</span><span class="n">pml</span><span class="o">.</span><span class="n">True_</span><span class="p">(),</span>
                    <span class="nb">id</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">estm_idx</span><span class="p">),</span>
                    <span class="n">TreeModel</span><span class="o">=</span><span class="n">pml</span><span class="o">.</span><span class="n">TreeModel</span><span class="p">(</span>
                        <span class="n">modelName</span><span class="o">=</span><span class="n">estm</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
                        <span class="n">functionName</span><span class="o">=</span><span class="n">get_mining_func</span><span class="p">(</span><span class="n">estm</span><span class="p">),</span>
                        <span class="n">splitCharacteristic</span><span class="o">=</span><span class="n">TREE_SPLIT_CHARACTERISTIC</span><span class="o">.</span><span class="n">MULTI</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                        <span class="n">MiningSchema</span><span class="o">=</span><span class="n">pml</span><span class="o">.</span><span class="n">MiningSchema</span><span class="p">(</span><span class="n">MiningField</span> <span class="o">=</span> <span class="n">mining_fields</span><span class="p">),</span>
                        <span class="n">Node</span><span class="o">=</span><span class="n">get_node</span><span class="p">(</span><span class="n">estm</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span> <span class="n">model</span><span class="p">)</span>
                    <span class="p">)</span>
                <span class="p">)</span>
            <span class="p">)</span>
    <span class="k">return</span> <span class="n">segments</span></div>


<div class="viewcode-block" id="get_classificationMethod"><span class="k">def</span> <span class="nf">get_classificationMethod</span><span class="p">(</span><span class="n">model</span><span class="p">):</span>
    
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It returns the Classification method name for SVM models.</span>
<span class="sd">    </span>
<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        A Scikit-learn model instance.</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    Returns the classification method of the SVM model</span>
<span class="sd">        </span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;SVC&#39;</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">SVM_CLASSIFICATION_METHOD</span><span class="o">.</span><span class="n">OVO</span><span class="o">.</span><span class="n">value</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">SVM_CLASSIFICATION_METHOD</span><span class="o">.</span><span class="n">OVR</span><span class="o">.</span><span class="n">value</span></div>


<div class="viewcode-block" id="get_vectorDictionary"><span class="k">def</span> <span class="nf">get_vectorDictionary</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span> <span class="n">categoric_values</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It return the Vector Dictionary element.</span>
<span class="sd">    </span>
<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        A Scikit-learn model instance.</span>
<span class="sd">    derived_col_names : List</span>
<span class="sd">        Contains column names after preprocessing.</span>
<span class="sd">    categoric_values : tuple</span>
<span class="sd">        Contains Categorical attribute names and its values</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    VectorDictionary :</span>
<span class="sd">        Nyoka&#39;s VectorDictionary object</span>
<span class="sd">        </span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">fieldref_element</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
    <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">derived_col_names</span><span class="p">:</span>
        <span class="n">fieldref_element</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">FieldRef</span><span class="p">(</span><span class="n">field</span><span class="o">=</span><span class="n">name</span><span class="p">))</span>
    
    <span class="n">vectorfields_element</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">VectorFields</span><span class="p">(</span><span class="n">FieldRef</span><span class="o">=</span><span class="n">fieldref_element</span><span class="p">)</span>
    <span class="n">vec_id</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">support_</span><span class="p">)</span>
    <span class="n">vecinsts</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
    <span class="n">vecs</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">support_vectors_</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="n">support_vectors_</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">!=</span> <span class="s1">&#39;csr_matrix&#39;</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">vec_idx</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">vecs</span><span class="p">)):</span>
            <span class="n">vecinsts</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">VectorInstance</span><span class="p">(</span>
                <span class="nb">id</span><span class="o">=</span><span class="n">vec_id</span><span class="p">[</span><span class="n">vec_idx</span><span class="p">],</span>
                <span class="n">REAL_SparseArray</span><span class="o">=</span><span class="n">pml</span><span class="o">.</span><span class="n">REAL_SparseArray</span><span class="p">(</span>
                    <span class="n">n</span><span class="o">=</span><span class="nb">len</span><span class="p">(</span><span class="n">fieldref_element</span><span class="p">),</span>
                    <span class="n">Indices</span><span class="o">=</span><span class="p">([</span><span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">vecs</span><span class="p">[</span><span class="n">vec_idx</span><span class="p">])</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)]),</span>
                    <span class="n">REAL_Entries</span><span class="o">=</span><span class="n">vecs</span><span class="p">[</span><span class="n">vec_idx</span><span class="p">]</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
                <span class="p">)</span>
            <span class="p">))</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">vec_idx</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">vecs</span><span class="p">)):</span>
            <span class="n">vecinsts</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">VectorInstance</span><span class="p">(</span>
                <span class="nb">id</span><span class="o">=</span><span class="n">vec_id</span><span class="p">[</span><span class="n">vec_idx</span><span class="p">],</span>
                <span class="n">REAL_SparseArray</span><span class="o">=</span><span class="n">pml</span><span class="o">.</span><span class="n">REAL_SparseArray</span><span class="p">(</span>
                    <span class="n">n</span><span class="o">=</span><span class="nb">len</span><span class="p">(</span><span class="n">fieldref_element</span><span class="p">),</span>
                    <span class="n">Indices</span><span class="o">=</span><span class="p">([</span><span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">vecs</span><span class="p">[</span><span class="n">vec_idx</span><span class="p">]</span><span class="o">.</span><span class="n">todense</span><span class="p">()</span><span class="o">.</span><span class="n">tolist</span><span class="p">()[</span><span class="mi">0</span><span class="p">])</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)]),</span>
                    <span class="n">REAL_Entries</span><span class="o">=</span><span class="n">vecs</span><span class="p">[</span><span class="n">vec_idx</span><span class="p">]</span><span class="o">.</span><span class="n">todense</span><span class="p">()</span><span class="o">.</span><span class="n">tolist</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span>
                <span class="p">)</span>
            <span class="p">))</span>
    <span class="n">vd</span><span class="o">=</span><span class="n">pml</span><span class="o">.</span><span class="n">VectorDictionary</span><span class="p">(</span><span class="n">VectorFields</span><span class="o">=</span><span class="n">vectorfields_element</span><span class="p">,</span> <span class="n">VectorInstance</span><span class="o">=</span><span class="n">vecinsts</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">vd</span></div>


<div class="viewcode-block" id="get_kernel_type"><span class="k">def</span> <span class="nf">get_kernel_type</span><span class="p">(</span><span class="n">model</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It returns the kernel type element.</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        A Scikit-learn model instance.</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    kernel_kwargs : Dictionary</span>
<span class="sd">        Get the respective kernel type of the SVM model.</span>
<span class="sd">        </span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">kernel_kwargs</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">()</span>
    <span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="n">kernel</span> <span class="o">==</span> <span class="s1">&#39;linear&#39;</span><span class="p">:</span>
        <span class="n">kernel_kwargs</span><span class="p">[</span><span class="s1">&#39;LinearKernelType&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">LinearKernelType</span><span class="p">(</span><span class="n">description</span><span class="o">=</span><span class="s1">&#39;Linear Kernel Type&#39;</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">model</span><span class="o">.</span><span class="n">kernel</span> <span class="o">==</span> <span class="s1">&#39;poly&#39;</span><span class="p">:</span>
        <span class="n">kernel_kwargs</span><span class="p">[</span><span class="s1">&#39;PolynomialKernelType&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">PolynomialKernelType</span><span class="p">(</span><span class="n">description</span><span class="o">=</span><span class="s1">&#39;Polynomial Kernel type&#39;</span><span class="p">,</span>
                                                                         <span class="n">gamma</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">{:.16f}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_gamma</span><span class="p">),</span>
                                                                         <span class="n">coef0</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">{:.16f}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">coef0</span><span class="p">),</span>
                                                                         <span class="n">degree</span><span class="o">=</span><span class="n">model</span><span class="o">.</span><span class="n">degree</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">model</span><span class="o">.</span><span class="n">kernel</span> <span class="o">==</span> <span class="s1">&#39;rbf&#39;</span><span class="p">:</span>
        <span class="n">kernel_kwargs</span><span class="p">[</span><span class="s1">&#39;RadialBasisKernelType&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">RadialBasisKernelType</span><span class="p">(</span><span class="n">description</span><span class="o">=</span><span class="s1">&#39;Radial Basis Kernel Type&#39;</span><span class="p">,</span>
                                                                           <span class="n">gamma</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">{:.16f}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_gamma</span><span class="p">))</span>
    <span class="k">elif</span> <span class="n">model</span><span class="o">.</span><span class="n">kernel</span> <span class="o">==</span> <span class="s1">&#39;sigmoid&#39;</span><span class="p">:</span>
        <span class="n">kernel_kwargs</span><span class="p">[</span><span class="s1">&#39;SigmoidKernelType&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">SigmoidKernelType</span><span class="p">(</span><span class="n">description</span><span class="o">=</span><span class="s1">&#39;Sigmoid Kernel Type&#39;</span><span class="p">,</span>
                                                               <span class="n">gamma</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">{:.16f}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">_gamma</span><span class="p">),</span>
                                                               <span class="n">coef0</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">{:.16f}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">coef0</span><span class="p">))</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">{}</span><span class="s2"> kernel is not implemented!&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">kernel</span><span class="p">))</span>
    <span class="k">return</span> <span class="n">kernel_kwargs</span></div>


<div class="viewcode-block" id="get_supportVectorMachine"><span class="k">def</span> <span class="nf">get_supportVectorMachine</span><span class="p">(</span><span class="n">model</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Generates PMML elements for support vector machine models</span>
<span class="sd">    </span>
<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        A Scikit-learn model instance.</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    support_vector_machines : List</span>
<span class="sd">        Nyoka&#39;s SupportVectorMachineModel object</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">support_vector_machines</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
    <span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;SVR&#39;</span><span class="p">,</span><span class="s1">&#39;OneClassSVM&#39;</span><span class="p">]:</span>
        <span class="n">support_vector</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">sv</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">support_</span><span class="p">:</span>
            <span class="n">support_vector</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">SupportVector</span><span class="p">(</span><span class="n">vectorId</span><span class="o">=</span><span class="n">sv</span><span class="p">))</span>
        <span class="n">support_vectors</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">SupportVectors</span><span class="p">(</span><span class="n">SupportVector</span><span class="o">=</span><span class="n">support_vector</span><span class="p">)</span>
        <span class="n">coefficient</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
        <span class="n">absoValue</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">intercept_</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="n">dual_coef_</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">!=</span> <span class="s1">&#39;csr_matrix&#39;</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">coef</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">dual_coef_</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">num</span> <span class="ow">in</span> <span class="n">coef</span><span class="p">:</span>
                    <span class="n">coefficient</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">Coefficient</span><span class="p">(</span><span class="n">value</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">{:.16f}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">num</span><span class="p">)))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">dual_coefficent</span><span class="o">=</span><span class="n">model</span><span class="o">.</span><span class="n">dual_coef_</span><span class="o">.</span><span class="n">data</span>
            <span class="k">for</span> <span class="n">num</span> <span class="ow">in</span> <span class="n">dual_coefficent</span><span class="p">:</span>
                <span class="n">coefficient</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">Coefficient</span><span class="p">(</span><span class="n">value</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">{:.16f}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">num</span><span class="p">)))</span>
        <span class="n">coeff</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">Coefficients</span><span class="p">(</span><span class="n">absoluteValue</span><span class="o">=</span><span class="n">absoValue</span><span class="p">,</span> <span class="n">Coefficient</span><span class="o">=</span><span class="n">coefficient</span><span class="p">)</span>
        <span class="n">support_vector_machines</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">SupportVectorMachine</span><span class="p">(</span><span class="n">SupportVectors</span><span class="o">=</span><span class="n">support_vectors</span><span class="p">,</span> <span class="n">Coefficients</span><span class="o">=</span><span class="n">coeff</span><span class="p">))</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
        <span class="n">support_vector_locs</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">cumsum</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">hstack</span><span class="p">([[</span><span class="mi">0</span><span class="p">],</span> <span class="n">model</span><span class="o">.</span><span class="n">n_support_</span><span class="p">]))</span>
        <span class="n">n_class</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">dual_coef_</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span>
        <span class="n">coef_abs_val_index</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">for</span> <span class="n">class1</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n_class</span><span class="p">):</span>
            <span class="n">sv1</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">support_</span><span class="p">[</span><span class="n">support_vector_locs</span><span class="p">[</span><span class="n">class1</span><span class="p">]:</span><span class="n">support_vector_locs</span><span class="p">[</span><span class="n">class1</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]]</span>
            <span class="k">for</span> <span class="n">class2</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">class1</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">n_class</span><span class="p">):</span>
                <span class="n">svs</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
                <span class="n">coefs</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
                <span class="n">sv2</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">support_</span><span class="p">[</span><span class="n">support_vector_locs</span><span class="p">[</span><span class="n">class2</span><span class="p">]:</span><span class="n">support_vector_locs</span><span class="p">[</span><span class="n">class2</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]]</span>
                <span class="n">svs</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="nb">list</span><span class="p">(</span><span class="n">sv1</span><span class="p">)</span> <span class="o">+</span> <span class="nb">list</span><span class="p">(</span><span class="n">sv2</span><span class="p">)))</span>
                <span class="n">alpha1</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">dual_coef_</span><span class="p">[</span><span class="n">class2</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="n">support_vector_locs</span><span class="p">[</span><span class="n">class1</span><span class="p">]:</span><span class="n">support_vector_locs</span><span class="p">[</span><span class="n">class1</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]]</span>
                <span class="n">alpha2</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">dual_coef_</span><span class="p">[</span><span class="n">class1</span><span class="p">,</span> <span class="n">support_vector_locs</span><span class="p">[</span><span class="n">class2</span><span class="p">]:</span><span class="n">support_vector_locs</span><span class="p">[</span><span class="n">class2</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]]</span>
                <span class="n">coefs</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="nb">list</span><span class="p">(</span><span class="n">alpha1</span><span class="p">)</span> <span class="o">+</span> <span class="nb">list</span><span class="p">(</span><span class="n">alpha2</span><span class="p">)))</span>
                <span class="n">all_svs</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
                <span class="k">for</span> <span class="n">sv</span> <span class="ow">in</span> <span class="p">(</span><span class="n">svs</span><span class="p">[</span><span class="mi">0</span><span class="p">]):</span>
                    <span class="n">all_svs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">SupportVector</span><span class="p">(</span><span class="n">vectorId</span><span class="o">=</span><span class="n">sv</span><span class="p">))</span>
                <span class="n">all_coefs</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
                <span class="k">for</span> <span class="n">coef</span> <span class="ow">in</span> <span class="p">(</span><span class="n">coefs</span><span class="p">[</span><span class="mi">0</span><span class="p">]):</span>
                    <span class="n">all_coefs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">Coefficient</span><span class="p">(</span><span class="n">value</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">{:.16f}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">coef</span><span class="p">)))</span>
                <span class="n">coef_abs_value</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">intercept_</span><span class="p">[</span><span class="n">coef_abs_val_index</span><span class="p">]</span>
                <span class="n">coef_abs_val_index</span> <span class="o">+=</span> <span class="mi">1</span>
                <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">classes_</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
                    <span class="n">support_vector_machines</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                        <span class="n">pml</span><span class="o">.</span><span class="n">SupportVectorMachine</span><span class="p">(</span>
                            <span class="n">targetCategory</span><span class="o">=</span><span class="n">model</span><span class="o">.</span><span class="n">classes_</span><span class="p">[</span><span class="n">class1</span><span class="p">],</span>
                            <span class="n">alternateTargetCategory</span><span class="o">=</span><span class="n">model</span><span class="o">.</span><span class="n">classes_</span><span class="p">[</span><span class="n">class2</span><span class="p">],</span>
                            <span class="n">SupportVectors</span><span class="o">=</span><span class="n">pml</span><span class="o">.</span><span class="n">SupportVectors</span><span class="p">(</span><span class="n">SupportVector</span><span class="o">=</span><span class="n">all_svs</span><span class="p">),</span>
                            <span class="n">Coefficients</span><span class="o">=</span><span class="n">pml</span><span class="o">.</span><span class="n">Coefficients</span><span class="p">(</span><span class="n">absoluteValue</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">{:.16f}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">coef_abs_value</span><span class="p">),</span> <span class="n">Coefficient</span><span class="o">=</span><span class="n">all_coefs</span><span class="p">)</span>
                        <span class="p">)</span>
                    <span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">support_vector_machines</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                        <span class="n">pml</span><span class="o">.</span><span class="n">SupportVectorMachine</span><span class="p">(</span>
                            <span class="n">targetCategory</span><span class="o">=</span><span class="n">model</span><span class="o">.</span><span class="n">classes_</span><span class="p">[</span><span class="n">class2</span><span class="p">],</span>
                            <span class="n">alternateTargetCategory</span><span class="o">=</span><span class="n">model</span><span class="o">.</span><span class="n">classes_</span><span class="p">[</span><span class="n">class1</span><span class="p">],</span>
                            <span class="n">SupportVectors</span><span class="o">=</span><span class="n">pml</span><span class="o">.</span><span class="n">SupportVectors</span><span class="p">(</span><span class="n">SupportVector</span><span class="o">=</span><span class="n">all_svs</span><span class="p">),</span>
                            <span class="n">Coefficients</span><span class="o">=</span><span class="n">pml</span><span class="o">.</span><span class="n">Coefficients</span><span class="p">(</span><span class="n">absoluteValue</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">{:.16f}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">coef_abs_value</span><span class="p">),</span> <span class="n">Coefficient</span><span class="o">=</span><span class="n">all_coefs</span><span class="p">)</span>
                        <span class="p">)</span>
                    <span class="p">)</span>
    <span class="k">return</span> <span class="n">support_vector_machines</span></div>


<div class="viewcode-block" id="get_tree_models"><span class="k">def</span> <span class="nf">get_tree_models</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">mining_imp_val</span><span class="p">,</span><span class="n">categoric_values</span><span class="p">,</span><span class="n">model_name</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Generates PMML elements for tree models</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        A Scikit-learn model instance.</span>
<span class="sd">    derived_col_names : </span>
<span class="sd">        Contains column names after preprocessing.</span>
<span class="sd">    col_names : List</span>
<span class="sd">        Contains list of feature/column names.    </span>
<span class="sd">    target_name : String</span>
<span class="sd">        Name of the Target column.    </span>
<span class="sd">    mining_imp_val : tuple</span>
<span class="sd">        Contains the mining_attributes,mining_strategy, mining_impute_value</span>
<span class="sd">    categoric_values : tuple</span>
<span class="sd">        Contains Categorical attribute names and its values</span>
<span class="sd">    model_name : string</span>
<span class="sd">        Name of the model</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    tree_models : List</span>
<span class="sd">        Nyoka&#39;s TreeModel object</span>
<span class="sd">        </span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">model_kwargs</span> <span class="o">=</span> <span class="n">get_model_kwargs</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">mining_imp_val</span><span class="p">,</span><span class="n">categoric_values</span><span class="p">)</span>
    <span class="n">tree_models</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
    <span class="n">tree_models</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">TreeModel</span><span class="p">(</span>
        <span class="n">modelName</span><span class="o">=</span><span class="n">model_name</span> <span class="k">if</span> <span class="n">model_name</span> <span class="k">else</span> <span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
        <span class="n">Node</span><span class="o">=</span><span class="n">get_node</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">),</span>
        <span class="o">**</span><span class="n">model_kwargs</span>
    <span class="p">))</span>
    <span class="k">return</span> <span class="n">tree_models</span></div>


<div class="viewcode-block" id="get_neural_models"><span class="k">def</span> <span class="nf">get_neural_models</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">mining_imp_val</span><span class="p">,</span> <span class="n">categoric_values</span><span class="p">,</span><span class="n">model_name</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Generates PMML elements for neural network models</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        A Scikit-learn model instance.</span>
<span class="sd">    derived_col_names : List</span>
<span class="sd">        Contains column names after preprocessing.</span>
<span class="sd">    col_names : List</span>
<span class="sd">        Contains list of feature/column names.    </span>
<span class="sd">    target_name : String</span>
<span class="sd">        Name of the Target column.    </span>
<span class="sd">    mining_imp_val : tuple</span>
<span class="sd">        Contains the mining_attributes,mining_strategy, mining_impute_value.</span>
<span class="sd">    categoric_values : tuple</span>
<span class="sd">        Contains Categorical attribute names and its values</span>
<span class="sd">    model_name : string</span>
<span class="sd">        Name of the model</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    neural_model : List</span>
<span class="sd">        Nyoka&#39;s NeuralNetwork object</span>
<span class="sd">        </span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">model_kwargs</span> <span class="o">=</span> <span class="n">get_model_kwargs</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">mining_imp_val</span><span class="p">,</span><span class="n">categoric_values</span><span class="p">)</span>
    <span class="n">neural_model</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
    <span class="n">neural_layers</span><span class="p">,</span> <span class="n">neural_outs</span> <span class="o">=</span> <span class="n">get_neural_layer</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">)</span>
    <span class="n">neural_model</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">NeuralNetwork</span><span class="p">(</span>
        <span class="n">modelName</span><span class="o">=</span><span class="n">model_name</span> <span class="k">if</span> <span class="n">model_name</span> <span class="k">else</span> <span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
        <span class="n">threshold</span><span class="o">=</span><span class="s1">&#39;0&#39;</span><span class="p">,</span>
        <span class="n">altitude</span><span class="o">=</span><span class="s1">&#39;1.0&#39;</span><span class="p">,</span>
        <span class="n">activationFunction</span><span class="o">=</span><span class="n">get_funct</span><span class="p">(</span><span class="n">model</span><span class="p">),</span>
        <span class="n">NeuralInputs</span> <span class="o">=</span> <span class="n">get_neuron_input</span><span class="p">(</span><span class="n">derived_col_names</span><span class="p">),</span>
        <span class="n">NeuralLayer</span> <span class="o">=</span> <span class="n">neural_layers</span><span class="p">,</span>
        <span class="n">NeuralOutputs</span> <span class="o">=</span> <span class="n">neural_outs</span><span class="p">,</span>
        <span class="o">**</span><span class="n">model_kwargs</span>
    <span class="p">))</span>
    <span class="k">return</span> <span class="n">neural_model</span></div>


<div class="viewcode-block" id="get_funct"><span class="k">def</span> <span class="nf">get_funct</span><span class="p">(</span><span class="n">sk_model</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It returns the activation fucntion for a neural network model.</span>
<span class="sd">    </span>
<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        A Scikit-learn model instance.</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    a_fn : String</span>
<span class="sd">        Returns the activation function.</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">a_fn</span> <span class="o">=</span> <span class="n">sk_model</span><span class="o">.</span><span class="n">activation</span>
    <span class="k">if</span> <span class="n">a_fn</span> <span class="o">==</span><span class="s1">&#39;relu&#39;</span><span class="p">:</span>
        <span class="n">a_fn</span> <span class="o">=</span> <span class="n">NN_ACTIVATION_FUNCTION</span><span class="o">.</span><span class="n">RECTIFIER</span><span class="o">.</span><span class="n">value</span>
    <span class="k">return</span> <span class="n">a_fn</span></div>


<div class="viewcode-block" id="get_regrs_models"><span class="k">def</span> <span class="nf">get_regrs_models</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">mining_imp_val</span><span class="p">,</span> <span class="n">categoric_values</span><span class="p">,</span><span class="n">model_name</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Generates PMML elements for linear models</span>
<span class="sd">    </span>
<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        A Scikit-learn model instance.</span>
<span class="sd">    derived_col_names : List</span>
<span class="sd">        Contains column names after preprocessing.</span>
<span class="sd">    col_names : List</span>
<span class="sd">        Contains list of feature/column names.    </span>
<span class="sd">    target_name : String</span>
<span class="sd">        Name of the Target column.    </span>
<span class="sd">    mining_imp_val : tuple</span>
<span class="sd">        Contains the mining_attributes,mining_strategy, mining_impute_value</span>
<span class="sd">    categoric_values : tuple</span>
<span class="sd">        Contains Categorical attribute names and its values</span>
<span class="sd">    model_name : string</span>
<span class="sd">        Name of the model</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    regrs_models : List</span>
<span class="sd">        Nyoka&#39;s RegressionModel object</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">model_kwargs</span> <span class="o">=</span> <span class="n">get_model_kwargs</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">mining_imp_val</span><span class="p">,</span> <span class="n">categoric_values</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="ow">not</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;LinearRegression&#39;</span><span class="p">,</span><span class="s1">&#39;LinearSVR&#39;</span><span class="p">]:</span> 
        <span class="n">model_kwargs</span><span class="p">[</span><span class="s1">&#39;normalizationMethod&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">REGRESSION_NORMALIZATION_METHOD</span><span class="o">.</span><span class="n">LOGISTIC</span><span class="o">.</span><span class="n">value</span>
    <span class="n">regrs_models</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
    <span class="n">regrs_models</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">RegressionModel</span><span class="p">(</span>
        <span class="n">modelName</span><span class="o">=</span><span class="n">model_name</span> <span class="k">if</span> <span class="n">model_name</span> <span class="k">else</span> <span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
        <span class="n">RegressionTable</span><span class="o">=</span><span class="n">get_regrs_tabl</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">derived_col_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">categoric_values</span><span class="p">),</span>
        <span class="o">**</span><span class="n">model_kwargs</span>
    <span class="p">))</span>
    <span class="k">return</span> <span class="n">regrs_models</span></div>


<div class="viewcode-block" id="get_regrs_tabl"><span class="k">def</span> <span class="nf">get_regrs_tabl</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">feature_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">categoric_values</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It returns the Regression Table element of the model.</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        A Scikit-learn model instance.</span>
<span class="sd">    derived_col_names : List</span>
<span class="sd">        Contains column names after preprocessing.</span>
<span class="sd">    target_name : String</span>
<span class="sd">        Name of the Target column.</span>
<span class="sd">    categoric_values : tuple</span>
<span class="sd">        Contains Categorical attribute names and its values</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    merge : List</span>
<span class="sd">        Nyoka&#39;s RegressionTable object</span>
<span class="sd">        </span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">merge</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
    <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="s1">&#39;intercept_&#39;</span><span class="p">):</span>
        <span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
        <span class="n">func_name</span> <span class="o">=</span> <span class="n">get_mining_func</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>
        <span class="n">inter</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">intercept_</span>
        <span class="n">model_coef</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">coef_</span>
        <span class="n">target_classes</span> <span class="o">=</span> <span class="n">target_name</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">inter</span><span class="p">,</span> <span class="s1">&#39;__iter__&#39;</span><span class="p">)</span> <span class="ow">or</span> <span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;LinearRegression&#39;</span><span class="p">,</span><span class="s1">&#39;LinearSVR&#39;</span><span class="p">]:</span>
            <span class="n">inter</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">inter</span><span class="p">])</span>
            <span class="n">target_classes</span> <span class="o">=</span> <span class="p">[</span><span class="n">target_classes</span><span class="p">]</span>
            <span class="n">model_coef</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ravel</span><span class="p">(</span><span class="n">model_coef</span><span class="p">)</span>
            <span class="n">model_coef</span> <span class="o">=</span> <span class="n">model_coef</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">model_coef</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
            <span class="n">target_cat</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">target_classes</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">classes_</span>
            <span class="n">max_target_index</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">target_classes</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span>
            <span class="n">target_cat</span> <span class="o">=</span> <span class="n">target_classes</span><span class="p">[</span><span class="n">max_target_index</span><span class="p">]</span>

        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">model_coef</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span><span class="s2">&quot;__len__&quot;</span><span class="p">):</span>
            <span class="n">model_coef</span> <span class="o">=</span> <span class="n">model_coef</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">reg_preds</span><span class="o">=</span><span class="nb">list</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">idx</span><span class="p">,</span> <span class="n">feat</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">feature_names</span><span class="p">):</span>
            <span class="n">reg_preds</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">NumericPredictor</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="n">feat</span><span class="p">,</span> <span class="n">coefficient</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">{:.16f}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">model_coef</span><span class="p">[</span><span class="n">idx</span><span class="p">])))</span>
        <span class="n">merge</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
            <span class="n">pml</span><span class="o">.</span><span class="n">RegressionTable</span><span class="p">(</span>
                <span class="n">intercept</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">{:.16f}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">inter</span><span class="o">.</span><span class="n">item</span><span class="p">()),</span>
                <span class="n">targetCategory</span><span class="o">=</span><span class="n">target_cat</span><span class="p">,</span>
                <span class="n">NumericPredictor</span><span class="o">=</span><span class="n">reg_preds</span>
            <span class="p">)</span>
        <span class="p">)</span>
        <span class="k">if</span> <span class="n">func_name</span> <span class="o">!=</span> <span class="n">MINING_FUNCTION</span><span class="o">.</span><span class="n">REGRESSION</span><span class="o">.</span><span class="n">value</span><span class="p">:</span>
            <span class="n">merge</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                <span class="n">pml</span><span class="o">.</span><span class="n">RegressionTable</span><span class="p">(</span>
                    <span class="n">intercept</span><span class="o">=</span><span class="s2">&quot;0.0&quot;</span><span class="p">,</span>
                    <span class="n">targetCategory</span><span class="o">=</span><span class="n">target_classes</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
                <span class="p">)</span>
            <span class="p">)</span>

    <span class="k">else</span><span class="p">:</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">classes_</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
            <span class="n">merge</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                <span class="n">pml</span><span class="o">.</span><span class="n">RegressionTable</span><span class="p">(</span>
                    <span class="n">NumericPredictor</span><span class="o">=</span><span class="p">[</span><span class="n">pml</span><span class="o">.</span><span class="n">NumericPredictor</span><span class="p">(</span><span class="n">coefficient</span><span class="o">=</span><span class="s1">&#39;1.0&#39;</span><span class="p">,</span><span class="n">name</span><span class="o">=</span><span class="n">feature_names</span><span class="p">[</span><span class="mi">0</span><span class="p">])],</span>
                    <span class="n">intercept</span><span class="o">=</span><span class="s1">&#39;0.0&#39;</span><span class="p">,</span>
                    <span class="n">targetCategory</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">classes_</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
                <span class="p">)</span>
            <span class="p">)</span>
            <span class="n">merge</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                <span class="n">pml</span><span class="o">.</span><span class="n">RegressionTable</span><span class="p">(</span><span class="n">intercept</span><span class="o">=</span><span class="s1">&#39;0.0&#39;</span><span class="p">,</span> <span class="n">targetCategory</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">classes_</span><span class="p">[</span><span class="mi">0</span><span class="p">]))</span>
            <span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">feat_idx</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">feature_names</span><span class="p">)):</span>
                <span class="n">merge</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                    <span class="n">pml</span><span class="o">.</span><span class="n">RegressionTable</span><span class="p">(</span>
                        <span class="n">NumericPredictor</span><span class="o">=</span><span class="p">[</span><span class="n">pml</span><span class="o">.</span><span class="n">NumericPredictor</span><span class="p">(</span><span class="n">coefficient</span><span class="o">=</span><span class="s1">&#39;1.0&#39;</span><span class="p">,</span><span class="n">name</span><span class="o">=</span><span class="n">feature_names</span><span class="p">[</span><span class="n">feat_idx</span><span class="p">])],</span>
                        <span class="n">intercept</span><span class="o">=</span><span class="s1">&#39;0.0&#39;</span><span class="p">,</span>
                        <span class="n">targetCategory</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">classes_</span><span class="p">[</span><span class="n">feat_idx</span><span class="p">])</span>
                    <span class="p">)</span>
                <span class="p">)</span>
    <span class="k">return</span> <span class="n">merge</span></div>



<div class="viewcode-block" id="get_node"><span class="k">def</span> <span class="nf">get_node</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">features_names</span><span class="p">,</span> <span class="n">main_model</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It return the Node element of the model.</span>
<span class="sd">    </span>
<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        An instance of the estimator of the tree object.</span>
<span class="sd">    features_names : List</span>
<span class="sd">        Contains the list of feature/column name.</span>
<span class="sd">    main_model :</span>
<span class="sd">        A Scikit-learn model instance.</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    Get all the underlying Nodes.</span>
<span class="sd">        </span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">tree</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">tree_</span>
    <span class="n">node_samples</span> <span class="o">=</span> <span class="n">tree</span><span class="o">.</span><span class="n">n_node_samples</span>
    <span class="k">if</span> <span class="n">main_model</span> <span class="ow">and</span> <span class="n">main_model</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;RandomForestClassifier&#39;</span><span class="p">:</span>
        <span class="n">classes</span> <span class="o">=</span> <span class="n">main_model</span><span class="o">.</span><span class="n">classes_</span>
    <span class="k">elif</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">model</span><span class="p">,</span><span class="s1">&#39;classes_&#39;</span><span class="p">):</span>
        <span class="n">classes</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">classes_</span>
    <span class="n">tree_leaf</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>

    <span class="k">def</span> <span class="nf">_getNode</span><span class="p">(</span><span class="n">idx</span><span class="p">,</span><span class="n">parent</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">cond</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="n">simple_pred_cond</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">if</span> <span class="n">cond</span><span class="p">:</span>
            <span class="n">simple_pred_cond</span> <span class="o">=</span> <span class="n">cond</span>
        <span class="n">node</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">Node</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="n">idx</span><span class="p">,</span> <span class="n">recordCount</span><span class="o">=</span><span class="nb">float</span><span class="p">(</span><span class="n">tree</span><span class="o">.</span><span class="n">n_node_samples</span><span class="p">[</span><span class="n">idx</span><span class="p">]))</span>
        <span class="k">if</span> <span class="n">simple_pred_cond</span><span class="p">:</span>
            <span class="n">node</span><span class="o">.</span><span class="n">SimplePredicate</span> <span class="o">=</span> <span class="n">simple_pred_cond</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">node</span><span class="o">.</span><span class="n">True_</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">True_</span><span class="p">()</span>


        <span class="k">if</span> <span class="n">tree</span><span class="o">.</span><span class="n">children_left</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span> <span class="o">!=</span> <span class="n">tree_leaf</span><span class="p">:</span>
            <span class="n">fieldName</span> <span class="o">=</span> <span class="n">features_names</span><span class="p">[</span><span class="n">tree</span><span class="o">.</span><span class="n">feature</span><span class="p">[</span><span class="n">idx</span><span class="p">]]</span>
            <span class="n">prnt</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">==</span> <span class="s2">&quot;ExtraTreeRegressor&quot;</span><span class="p">:</span>
                <span class="n">prnt</span> <span class="o">=</span> <span class="n">parent</span> <span class="o">+</span> <span class="mi">1</span>
            <span class="n">thresh</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">rnd_</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">tree</span><span class="o">.</span><span class="n">threshold</span><span class="p">[</span><span class="n">idx</span><span class="p">])</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;.&quot;</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span>
                <span class="n">thresh</span> <span class="o">=</span> <span class="nb">round</span><span class="p">(</span><span class="n">tree</span><span class="o">.</span><span class="n">threshold</span><span class="p">[</span><span class="n">idx</span><span class="p">],</span> <span class="nb">min</span><span class="p">(</span><span class="n">rnd_</span><span class="p">,</span> <span class="mi">16</span><span class="p">))</span>
            <span class="k">except</span><span class="p">:</span>
                <span class="n">thresh</span> <span class="o">=</span> <span class="n">tree</span><span class="o">.</span><span class="n">threshold</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span>
            <span class="n">simplePredicate</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">SimplePredicate</span><span class="p">(</span><span class="n">field</span><span class="o">=</span><span class="n">fieldName</span><span class="p">,</span> <span class="n">operator</span><span class="o">=</span><span class="n">SIMPLE_PREDICATE_OPERATOR</span><span class="o">.</span><span class="n">LESS_OR_EQUAL</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>\
                <span class="n">value</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">thresh</span><span class="p">))</span>
                                                <span class="c1">#   value=&quot;{:.16f}&quot;.format(tree.threshold[idx]))</span>
            <span class="n">left_child</span> <span class="o">=</span> <span class="n">_getNode</span><span class="p">(</span><span class="n">tree</span><span class="o">.</span><span class="n">children_left</span><span class="p">[</span><span class="n">idx</span><span class="p">],</span><span class="n">prnt</span><span class="p">,</span> <span class="n">simplePredicate</span><span class="p">)</span>
            <span class="n">simplePredicate</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">SimplePredicate</span><span class="p">(</span><span class="n">field</span><span class="o">=</span><span class="n">fieldName</span><span class="p">,</span> <span class="n">operator</span><span class="o">=</span><span class="n">SIMPLE_PREDICATE_OPERATOR</span><span class="o">.</span><span class="n">GREATER_THAN</span><span class="o">.</span><span class="n">value</span><span class="p">,</span> \
                <span class="n">value</span><span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">thresh</span><span class="p">))</span>
                                                <span class="c1">#   value=&quot;{:.16f}&quot;.format(tree.threshold[idx]))</span>
            <span class="n">right_child</span> <span class="o">=</span> <span class="n">_getNode</span><span class="p">(</span><span class="n">tree</span><span class="o">.</span><span class="n">children_right</span><span class="p">[</span><span class="n">idx</span><span class="p">],</span><span class="n">prnt</span><span class="p">,</span> <span class="n">simplePredicate</span><span class="p">)</span>
            <span class="n">node</span><span class="o">.</span><span class="n">add_Node</span><span class="p">(</span><span class="n">left_child</span><span class="p">)</span>
            <span class="n">node</span><span class="o">.</span><span class="n">add_Node</span><span class="p">(</span><span class="n">right_child</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">nodeValue</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">tree</span><span class="o">.</span><span class="n">value</span><span class="p">[</span><span class="n">idx</span><span class="p">][</span><span class="mi">0</span><span class="p">])</span>
            <span class="n">lSum</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="nb">sum</span><span class="p">(</span><span class="n">nodeValue</span><span class="p">))</span>
            <span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;DecisionTreeClassifier&#39;</span><span class="p">:</span>
                <span class="n">probs</span> <span class="o">=</span> <span class="p">[</span><span class="n">x</span> <span class="o">/</span> <span class="n">lSum</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">nodeValue</span><span class="p">]</span>
                <span class="n">score_dst</span> <span class="o">=</span> <span class="p">[]</span>
                <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">probs</span><span class="p">)):</span>
                    <span class="n">score_dst</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">ScoreDistribution</span><span class="p">(</span><span class="n">confidence</span><span class="o">=</span><span class="n">probs</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">recordCount</span><span class="o">=</span><span class="nb">float</span><span class="p">(</span><span class="n">nodeValue</span><span class="p">[</span><span class="n">i</span><span class="p">]),</span>
                                                          <span class="n">value</span><span class="o">=</span><span class="n">classes</span><span class="p">[</span><span class="n">i</span><span class="p">]))</span>
                <span class="n">node</span><span class="o">.</span><span class="n">ScoreDistribution</span> <span class="o">=</span> <span class="n">score_dst</span>
                <span class="n">node</span><span class="o">.</span><span class="n">score</span> <span class="o">=</span> <span class="n">classes</span><span class="p">[</span><span class="n">probs</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="nb">max</span><span class="p">(</span><span class="n">probs</span><span class="p">))]</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">==</span> <span class="s2">&quot;ExtraTreeRegressor&quot;</span><span class="p">:</span>
                    <span class="n">nd_sam</span><span class="o">=</span><span class="n">node_samples</span><span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="n">idx</span><span class="p">)]</span>
                    <span class="n">node</span><span class="o">.</span><span class="n">score</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="si">{:.16f}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">parent</span><span class="o">+</span><span class="n">avgPathLength</span><span class="p">(</span><span class="n">nd_sam</span><span class="p">))</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">node</span><span class="o">.</span><span class="n">score</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">{:.16f}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">lSum</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">node</span>
    <span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">==</span> <span class="s2">&quot;ExtraTreeRegressor&quot;</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">_getNode</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">_getNode</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span></div>

<div class="viewcode-block" id="avgPathLength"><span class="k">def</span> <span class="nf">avgPathLength</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Generates average path length for Isolation forest models</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    n : int</span>
<span class="sd">        Number of samples</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    The average path length</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">n</span><span class="o">&lt;=</span><span class="mf">1.0</span><span class="p">:</span>
        <span class="k">return</span> <span class="mf">1.0</span>
    <span class="k">return</span> <span class="mf">2.0</span><span class="o">*</span><span class="p">(</span><span class="n">math</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">n</span><span class="o">-</span><span class="mf">1.0</span><span class="p">)</span><span class="o">+</span><span class="mf">0.57721566</span><span class="p">)</span> <span class="o">-</span> <span class="mf">2.0</span><span class="o">*</span><span class="p">((</span><span class="n">n</span><span class="o">-</span><span class="mf">1.0</span><span class="p">)</span><span class="o">/</span><span class="n">n</span><span class="p">)</span></div>


<div class="viewcode-block" id="get_output"><span class="k">def</span> <span class="nf">get_output</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">target_name</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It returns the output element of the model.</span>
<span class="sd">    </span>
<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        A Scikit-learn model instance.</span>
<span class="sd">    target_name : String</span>
<span class="sd">        Name of the Target column.</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    Output :</span>
<span class="sd">        Nyoka&#39;s Output object</span>
<span class="sd">        </span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">mining_func</span> <span class="o">=</span> <span class="n">get_mining_func</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>
    <span class="n">output_fields</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">has_target</span><span class="p">(</span><span class="n">model</span><span class="p">):</span>
        <span class="n">output_fields</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">OutputField</span><span class="p">(</span>
                <span class="n">name</span><span class="o">=</span><span class="s1">&#39;predicted&#39;</span><span class="p">,</span>
                <span class="n">feature</span><span class="o">=</span><span class="n">RESULT_FEATURE</span><span class="o">.</span><span class="n">PREDICTED_VALUE</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                <span class="n">optype</span><span class="o">=</span><span class="n">OPTYPE</span><span class="o">.</span><span class="n">CONTINUOUS</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                <span class="n">dataType</span><span class="o">=</span><span class="n">DATATYPE</span><span class="o">.</span><span class="n">DOUBLE</span><span class="o">.</span><span class="n">value</span>
            <span class="p">))</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">alt_target_name</span> <span class="o">=</span> <span class="s1">&#39;predicted_&#39;</span> <span class="o">+</span> <span class="n">target_name</span>
        <span class="k">if</span> <span class="n">mining_func</span> <span class="o">==</span> <span class="n">MINING_FUNCTION</span><span class="o">.</span><span class="n">CLASSIFICATION</span><span class="o">.</span><span class="n">value</span><span class="p">:</span>
            <span class="k">for</span> <span class="bp">cls</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">classes_</span><span class="p">:</span>
                <span class="n">output_fields</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">OutputField</span><span class="p">(</span>
                    <span class="n">name</span><span class="o">=</span><span class="s1">&#39;probability_&#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="bp">cls</span><span class="p">),</span>
                    <span class="n">feature</span><span class="o">=</span><span class="n">RESULT_FEATURE</span><span class="o">.</span><span class="n">PROBABILITY</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                    <span class="n">optype</span><span class="o">=</span><span class="n">OPTYPE</span><span class="o">.</span><span class="n">CONTINUOUS</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                    <span class="n">dataType</span><span class="o">=</span><span class="n">DATATYPE</span><span class="o">.</span><span class="n">DOUBLE</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                    <span class="n">value</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="bp">cls</span><span class="p">)</span>
                <span class="p">))</span>
            <span class="n">output_fields</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">OutputField</span><span class="p">(</span>
                <span class="n">name</span><span class="o">=</span><span class="n">alt_target_name</span><span class="p">,</span>
                <span class="n">feature</span><span class="o">=</span><span class="n">RESULT_FEATURE</span><span class="o">.</span><span class="n">PREDICTED_VALUE</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                <span class="n">optype</span><span class="o">=</span><span class="n">OPTYPE</span><span class="o">.</span><span class="n">CATEGORICAL</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                <span class="n">dataType</span><span class="o">=</span><span class="n">get_dtype</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">classes_</span><span class="p">[</span><span class="mi">0</span><span class="p">])))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">output_fields</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">OutputField</span><span class="p">(</span>
                <span class="n">name</span><span class="o">=</span><span class="n">alt_target_name</span><span class="p">,</span>
                <span class="n">feature</span><span class="o">=</span><span class="n">RESULT_FEATURE</span><span class="o">.</span><span class="n">PREDICTED_VALUE</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                <span class="n">optype</span><span class="o">=</span><span class="n">OPTYPE</span><span class="o">.</span><span class="n">CONTINUOUS</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                <span class="n">dataType</span><span class="o">=</span><span class="n">DATATYPE</span><span class="o">.</span><span class="n">DOUBLE</span><span class="o">.</span><span class="n">value</span><span class="p">))</span>
    <span class="k">return</span> <span class="n">pml</span><span class="o">.</span><span class="n">Output</span><span class="p">(</span><span class="n">OutputField</span><span class="o">=</span><span class="n">output_fields</span><span class="p">)</span></div>




<div class="viewcode-block" id="get_mining_func"><span class="k">def</span> <span class="nf">get_mining_func</span><span class="p">(</span><span class="n">model</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It returns the name of the mining function of the model.</span>
<span class="sd">    </span>
<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        A Scikit-learn model instance.</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    func_name : String</span>
<span class="sd">        Returns the function name of the model</span>
<span class="sd">        </span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="s1">&#39;classes_&#39;</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">model</span><span class="p">,</span><span class="s1">&#39;n_clusters&#39;</span><span class="p">):</span>
            <span class="n">func_name</span> <span class="o">=</span> <span class="n">MINING_FUNCTION</span><span class="o">.</span><span class="n">CLUSTERING</span><span class="o">.</span><span class="n">value</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">func_name</span> <span class="o">=</span> <span class="n">MINING_FUNCTION</span><span class="o">.</span><span class="n">REGRESSION</span><span class="o">.</span><span class="n">value</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">classes_</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">):</span>
            <span class="n">func_name</span> <span class="o">=</span> <span class="n">MINING_FUNCTION</span><span class="o">.</span><span class="n">CLASSIFICATION</span><span class="o">.</span><span class="n">value</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">func_name</span> <span class="o">=</span> <span class="n">MINING_FUNCTION</span><span class="o">.</span><span class="n">REGRESSION</span><span class="o">.</span><span class="n">value</span>

    <span class="k">return</span> <span class="n">func_name</span></div>


<div class="viewcode-block" id="get_mining_schema"><span class="k">def</span> <span class="nf">get_mining_schema</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">feature_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">mining_imp_val</span><span class="p">,</span> <span class="n">categoric_values</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It returns the Mining Schema of the model.</span>
<span class="sd">    </span>
<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        A Scikit-learn model instance.</span>
<span class="sd">    feature_names : List</span>
<span class="sd">        Contains the list of feature/column name.</span>
<span class="sd">    target_name : String</span>
<span class="sd">        Name of the Target column.</span>
<span class="sd">    mining_imp_val : tuple</span>
<span class="sd">        Contains the mining_attributes,mining_strategy, mining_impute_value.</span>
<span class="sd">    categoric_values : tuple</span>
<span class="sd">        Contains Categorical attribute names and its values</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    MiningSchema :</span>
<span class="sd">        Nyoka&#39;s MiningSchema object</span>
<span class="sd">        </span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">mining_imp_val</span><span class="p">:</span>
        <span class="n">mining_attributes</span> <span class="o">=</span> <span class="n">mining_imp_val</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">mining_strategy</span> <span class="o">=</span> <span class="n">mining_imp_val</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">mining_replacement_val</span> <span class="o">=</span> <span class="n">mining_imp_val</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
    <span class="n">n_features</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">feature_names</span><span class="p">)</span>
    <span class="n">features_pmml_optype</span> <span class="o">=</span> <span class="p">[</span><span class="n">OPTYPE</span><span class="o">.</span><span class="n">CONTINUOUS</span><span class="o">.</span><span class="n">value</span><span class="p">]</span> <span class="o">*</span> <span class="n">n_features</span>
    <span class="n">features_pmml_utype</span> <span class="o">=</span> <span class="p">[</span><span class="n">FIELD_USAGE_TYPE</span><span class="o">.</span><span class="n">ACTIVE</span><span class="o">.</span><span class="n">value</span><span class="p">]</span> <span class="o">*</span> <span class="n">n_features</span>
    <span class="n">target_pmml_utype</span> <span class="o">=</span> <span class="n">FIELD_USAGE_TYPE</span><span class="o">.</span><span class="n">TARGET</span><span class="o">.</span><span class="n">value</span>
    <span class="n">mining_func</span> <span class="o">=</span> <span class="n">get_mining_func</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">mining_func</span> <span class="o">==</span> <span class="n">MINING_FUNCTION</span><span class="o">.</span><span class="n">CLASSIFICATION</span><span class="o">.</span><span class="n">value</span><span class="p">:</span>
        <span class="n">target_pmml_optype</span> <span class="o">=</span> <span class="n">OPTYPE</span><span class="o">.</span><span class="n">CATEGORICAL</span><span class="o">.</span><span class="n">value</span>
    <span class="k">elif</span> <span class="n">mining_func</span> <span class="o">==</span> <span class="n">MINING_FUNCTION</span><span class="o">.</span><span class="n">REGRESSION</span><span class="o">.</span><span class="n">value</span><span class="p">:</span>
        <span class="n">target_pmml_optype</span> <span class="o">=</span> <span class="n">OPTYPE</span><span class="o">.</span><span class="n">CONTINUOUS</span><span class="o">.</span><span class="n">value</span>
    <span class="n">mining_flds</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
    <span class="n">mining_name_stored</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
    <span class="c1"># handling impute pre processing</span>
    <span class="k">if</span> <span class="n">mining_imp_val</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">mining_item</span><span class="p">,</span> <span class="n">mining_idx</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">mining_attributes</span><span class="p">,</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">mining_attributes</span><span class="p">))):</span>
            <span class="k">for</span> <span class="n">feat_name</span><span class="p">,</span><span class="n">feat_idx</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">feature_names</span><span class="p">,</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">feature_names</span><span class="p">))):</span>
                <span class="k">if</span> <span class="n">feat_name</span> <span class="ow">in</span> <span class="n">mining_item</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">feat_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">mining_name_stored</span><span class="p">:</span>
                        <span class="n">impute_index</span> <span class="o">=</span> <span class="n">mining_item</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">feat_name</span><span class="p">)</span>

                        <span class="n">mining_flds</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">MiningField</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">feat_name</span><span class="p">),</span>
                                                           <span class="n">optype</span><span class="o">=</span><span class="n">features_pmml_optype</span><span class="p">[</span><span class="n">feat_idx</span><span class="p">],</span>
                                                           <span class="n">missingValueReplacement</span><span class="o">=</span><span class="n">mining_replacement_val</span><span class="p">[</span><span class="n">mining_idx</span><span class="p">][</span>
                                                              <span class="n">impute_index</span><span class="p">],</span>
                                                           <span class="n">missingValueTreatment</span><span class="o">=</span><span class="n">mining_strategy</span><span class="p">[</span><span class="n">mining_idx</span><span class="p">],</span>
                                                           <span class="n">usageType</span><span class="o">=</span><span class="n">features_pmml_utype</span><span class="p">[</span><span class="n">feat_idx</span><span class="p">]))</span>
                        <span class="n">mining_name_stored</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">feat_name</span><span class="p">)</span>
    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">categoric_values</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">cls_attr</span> <span class="ow">in</span> <span class="n">categoric_values</span><span class="p">[</span><span class="mi">1</span><span class="p">]:</span>
            <span class="n">mining_flds</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">MiningField</span><span class="p">(</span>
                <span class="n">name</span><span class="o">=</span><span class="n">cls_attr</span><span class="p">,</span>
                <span class="n">usageType</span><span class="o">=</span><span class="n">FIELD_USAGE_TYPE</span><span class="o">.</span><span class="n">ACTIVE</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                <span class="n">optype</span><span class="o">=</span><span class="n">OPTYPE</span><span class="o">.</span><span class="n">CATEGORICAL</span><span class="o">.</span><span class="n">value</span>
            <span class="p">))</span>
            <span class="n">mining_name_stored</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">cls_attr</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">feat_name</span><span class="p">,</span> <span class="n">feat_idx</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">feature_names</span><span class="p">,</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">feature_names</span><span class="p">))):</span>
        <span class="k">if</span> <span class="n">feat_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">mining_name_stored</span><span class="p">:</span>
            <span class="n">mining_flds</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">MiningField</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">feat_name</span><span class="p">),</span>
                                               <span class="n">optype</span><span class="o">=</span><span class="n">features_pmml_optype</span><span class="p">[</span><span class="n">feat_idx</span><span class="p">],</span>
                                               <span class="n">usageType</span><span class="o">=</span><span class="n">features_pmml_utype</span><span class="p">[</span><span class="n">feat_idx</span><span class="p">]))</span>
    <span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="ow">not</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;KMeans&#39;</span><span class="p">,</span> <span class="s1">&#39;IsolationForest&#39;</span><span class="p">,</span> <span class="s1">&#39;OneClassSVM&#39;</span><span class="p">]:</span>
        <span class="n">mining_flds</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">MiningField</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="n">target_name</span><span class="p">,</span>
                                        <span class="n">optype</span><span class="o">=</span><span class="n">target_pmml_optype</span><span class="p">,</span>
                                            <span class="n">usageType</span><span class="o">=</span><span class="n">target_pmml_utype</span><span class="p">))</span>
    <span class="k">return</span> <span class="n">pml</span><span class="o">.</span><span class="n">MiningSchema</span><span class="p">(</span><span class="n">MiningField</span><span class="o">=</span><span class="n">mining_flds</span><span class="p">)</span></div>


<div class="viewcode-block" id="get_neuron_input"><span class="k">def</span> <span class="nf">get_neuron_input</span><span class="p">(</span><span class="n">feature_names</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It returns the Neural Input element.</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    feature_names : List</span>
<span class="sd">        Contains the list of feature/column name. </span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    neural_input_element :</span>
<span class="sd">        Returns Nyoka&#39;s NeuralInput object</span>
<span class="sd">        </span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">neural_input</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
    <span class="k">for</span> <span class="n">features</span> <span class="ow">in</span> <span class="n">feature_names</span><span class="p">:</span>
        <span class="n">field_ref</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">FieldRef</span><span class="p">(</span><span class="n">field</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">features</span><span class="p">))</span>
        <span class="n">derived_flds</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">DerivedField</span><span class="p">(</span><span class="n">optype</span> <span class="o">=</span> <span class="n">OPTYPE</span><span class="o">.</span><span class="n">CONTINUOUS</span><span class="o">.</span><span class="n">value</span><span class="p">,</span> <span class="n">dataType</span> <span class="o">=</span> <span class="n">DATATYPE</span><span class="o">.</span><span class="n">DOUBLE</span><span class="o">.</span><span class="n">value</span><span class="p">,</span> <span class="n">FieldRef</span> <span class="o">=</span> <span class="n">field_ref</span><span class="p">)</span>
        <span class="n">class_node</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">NeuralInput</span><span class="p">(</span><span class="nb">id</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">features</span><span class="p">),</span> <span class="n">DerivedField</span> <span class="o">=</span> <span class="n">derived_flds</span><span class="p">)</span>
        <span class="n">neural_input</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">class_node</span><span class="p">)</span>
    <span class="n">neural_input_element</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">NeuralInputs</span><span class="p">(</span><span class="n">NeuralInput</span> <span class="o">=</span> <span class="n">neural_input</span><span class="p">,</span> <span class="n">numberOfInputs</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">neural_input</span><span class="p">)))</span>
    <span class="k">return</span> <span class="n">neural_input_element</span></div>


<div class="viewcode-block" id="get_neural_layer"><span class="k">def</span> <span class="nf">get_neural_layer</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">feature_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It returns the Neural Layer and Neural Ouptput element.</span>
<span class="sd">    </span>
<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        A Scikit-learn model instance.</span>
<span class="sd">    feature_names : List</span>
<span class="sd">        Contains the list of feature/column name. </span>
<span class="sd">    target_name : String</span>
<span class="sd">        Name of the Target column.</span>
<span class="sd">    </span>
<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    all_neuron_layer : List</span>
<span class="sd">        Nyoka&#39;s NeuralLayer object</span>

<span class="sd">    neural_output_element :</span>
<span class="sd">        Nyoka&#39;s NeuralOutput object</span>
<span class="sd">        </span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">weight</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">coefs_</span>
    <span class="n">bias</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">intercepts_</span>
    <span class="n">last_layer</span> <span class="o">=</span> <span class="n">bias</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
    <span class="n">hidden_layer_sizes</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">hidden_layer_sizes</span>
    <span class="n">hidden_layers</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">hidden_layer_sizes</span><span class="p">)</span>
    <span class="n">hidden_layers</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">last_layer</span><span class="p">))</span>
    <span class="n">neuron</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
    <span class="n">all_neuron_layer</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
    <span class="n">input_features</span> <span class="o">=</span> <span class="n">feature_names</span>
    <span class="n">neuron_id</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
    <span class="k">for</span> <span class="n">count</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">hidden_layers</span><span class="p">)):</span>
        <span class="k">for</span> <span class="n">count1</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">hidden_layers</span><span class="p">[</span><span class="n">count</span><span class="p">]):</span>
            <span class="n">con</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
            <span class="k">for</span> <span class="n">count2</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">input_features</span><span class="p">)):</span>
                <span class="n">con</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">Con</span><span class="p">(</span><span class="n">from_</span> <span class="o">=</span> <span class="n">input_features</span><span class="p">[</span><span class="n">count2</span><span class="p">],</span> <span class="n">weight</span> <span class="o">=</span> <span class="nb">format</span><span class="p">(</span><span class="n">weight</span><span class="p">[</span><span class="n">count</span><span class="p">][</span><span class="n">count2</span><span class="p">][</span><span class="n">count1</span><span class="p">])))</span>
            <span class="n">neuron</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">Neuron</span><span class="p">(</span><span class="nb">id</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">count</span><span class="p">)</span><span class="o">+</span><span class="nb">str</span><span class="p">(</span><span class="n">count1</span><span class="p">),</span> <span class="n">bias</span> <span class="o">=</span> <span class="nb">format</span><span class="p">(</span><span class="n">bias</span><span class="p">[</span><span class="n">count</span><span class="p">][</span><span class="n">count1</span><span class="p">]),</span><span class="n">Con</span> <span class="o">=</span> <span class="n">con</span><span class="p">))</span>
            <span class="n">neuron_id</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">count</span><span class="p">)</span><span class="o">+</span><span class="nb">str</span><span class="p">(</span><span class="n">count1</span><span class="p">))</span>
        <span class="n">all_neuron_layer</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">NeuralLayer</span><span class="p">(</span><span class="n">Neuron</span> <span class="o">=</span> <span class="n">neuron</span><span class="p">))</span>
        <span class="n">input_features</span> <span class="o">=</span> <span class="n">neuron_id</span>
        <span class="n">neuron_id</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
        <span class="n">neuron</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
    <span class="n">all_neuron_layer</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">activationFunction</span> <span class="o">=</span> <span class="n">NN_ACTIVATION_FUNCTION</span><span class="o">.</span><span class="n">IDENTITY</span><span class="o">.</span><span class="n">value</span>
    <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="s2">&quot;classes_&quot;</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">classes_</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
            <span class="n">bias1</span><span class="o">=</span><span class="p">[</span><span class="mf">1.0</span><span class="p">,</span><span class="mf">0.0</span><span class="p">]</span>
            <span class="n">weight1</span><span class="o">=</span><span class="p">[</span><span class="o">-</span><span class="mf">1.0</span><span class="p">,</span><span class="mf">1.0</span><span class="p">]</span>
            <span class="n">con</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
            <span class="n">linear</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;logistic/1&#39;</span><span class="p">]</span>
            <span class="n">i_d</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;false&#39;</span><span class="p">,</span> <span class="s1">&#39;true&#39;</span><span class="p">]</span>
            <span class="n">con</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">Con</span><span class="p">(</span><span class="n">from_</span> <span class="o">=</span> <span class="n">input_features</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">weight</span> <span class="o">=</span> <span class="mf">1.0</span><span class="p">))</span>
            <span class="n">neuron</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">Neuron</span><span class="p">(</span><span class="nb">id</span> <span class="o">=</span> <span class="n">linear</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">bias</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;0.0&#39;</span><span class="p">),</span> <span class="n">Con</span> <span class="o">=</span> <span class="n">con</span><span class="p">))</span>
            <span class="n">all_neuron_layer</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">NeuralLayer</span><span class="p">(</span><span class="n">activationFunction</span> <span class="o">=</span> <span class="n">NN_ACTIVATION_FUNCTION</span><span class="o">.</span><span class="n">LOGISTIC</span><span class="o">.</span><span class="n">value</span><span class="p">,</span> <span class="n">Neuron</span> <span class="o">=</span> <span class="n">neuron</span><span class="p">))</span>
            <span class="n">neuron</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
            <span class="n">con</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
            <span class="k">for</span> <span class="n">num</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">):</span>
                <span class="n">con</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">Con</span><span class="p">(</span><span class="n">from_</span> <span class="o">=</span> <span class="n">linear</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">weight</span> <span class="o">=</span> <span class="nb">format</span><span class="p">(</span><span class="n">weight1</span><span class="p">[</span><span class="n">num</span><span class="p">])))</span>
                <span class="n">neuron</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">Neuron</span><span class="p">(</span><span class="nb">id</span> <span class="o">=</span> <span class="n">i_d</span><span class="p">[</span><span class="n">num</span><span class="p">],</span> <span class="n">bias</span> <span class="o">=</span> <span class="nb">format</span><span class="p">(</span><span class="n">bias1</span><span class="p">[</span><span class="n">num</span><span class="p">]),</span> <span class="n">Con</span> <span class="o">=</span> <span class="n">con</span><span class="p">))</span>
                <span class="n">con</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
            <span class="n">all_neuron_layer</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">NeuralLayer</span><span class="p">(</span><span class="n">activationFunction</span> <span class="o">=</span> <span class="n">NN_ACTIVATION_FUNCTION</span><span class="o">.</span><span class="n">IDENTITY</span><span class="o">.</span><span class="n">value</span><span class="p">,</span> <span class="n">Neuron</span> <span class="o">=</span> <span class="n">neuron</span><span class="p">))</span>
            <span class="n">input_features</span> <span class="o">=</span> <span class="n">i_d</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">all_neuron_layer</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">normalizationMethod</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">out_activation_</span>
        
        
        <span class="n">neural_output</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">values</span><span class="p">,</span> <span class="n">count</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">classes_</span><span class="p">,</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">classes_</span><span class="p">))):</span>
            <span class="n">norm_discrete</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">NormDiscrete</span><span class="p">(</span><span class="n">field</span> <span class="o">=</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">value</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">values</span><span class="p">))</span>
            <span class="n">derived_flds</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">DerivedField</span><span class="p">(</span><span class="n">optype</span> <span class="o">=</span> <span class="n">OPTYPE</span><span class="o">.</span><span class="n">CATEGORICAL</span><span class="o">.</span><span class="n">value</span><span class="p">,</span> <span class="n">dataType</span> <span class="o">=</span> <span class="n">DATATYPE</span><span class="o">.</span><span class="n">DOUBLE</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                                    <span class="n">NormDiscrete</span> <span class="o">=</span> <span class="n">norm_discrete</span><span class="p">)</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">input_features</span><span class="p">)</span><span class="o">==</span><span class="mi">1</span><span class="p">:</span>
                <span class="n">class_node</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">NeuralOutput</span><span class="p">(</span><span class="n">outputNeuron</span> <span class="o">=</span> <span class="n">input_features</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">DerivedField</span> <span class="o">=</span> <span class="n">derived_flds</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">class_node</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">NeuralOutput</span><span class="p">(</span><span class="n">outputNeuron</span> <span class="o">=</span> <span class="n">input_features</span><span class="p">[</span><span class="n">count</span><span class="p">],</span><span class="n">DerivedField</span> <span class="o">=</span> <span class="n">derived_flds</span><span class="p">)</span>
            <span class="n">neural_output</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">class_node</span><span class="p">)</span>
        <span class="n">neural_output_element</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">NeuralOutputs</span><span class="p">(</span><span class="n">numberOfOutputs</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="n">Extension</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
                                                    <span class="n">NeuralOutput</span> <span class="o">=</span> <span class="n">neural_output</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">neural_output</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
        <span class="n">fieldRef</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">FieldRef</span><span class="p">(</span><span class="n">field</span> <span class="o">=</span> <span class="n">target_name</span><span class="p">)</span>
        <span class="n">derived_flds</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">DerivedField</span><span class="p">(</span><span class="n">optype</span> <span class="o">=</span> <span class="n">OPTYPE</span><span class="o">.</span><span class="n">CONTINUOUS</span><span class="o">.</span><span class="n">value</span><span class="p">,</span> <span class="n">dataType</span> <span class="o">=</span> <span class="n">DATATYPE</span><span class="o">.</span><span class="n">DOUBLE</span><span class="o">.</span><span class="n">value</span><span class="p">,</span> <span class="n">FieldRef</span> <span class="o">=</span> <span class="n">fieldRef</span><span class="p">)</span>
        <span class="n">class_node</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">NeuralOutput</span><span class="p">(</span><span class="n">outputNeuron</span> <span class="o">=</span> <span class="n">input_features</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">DerivedField</span> <span class="o">=</span> <span class="n">derived_flds</span><span class="p">)</span>
        <span class="n">neural_output</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">class_node</span><span class="p">)</span>
        <span class="n">neural_output_element</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">NeuralOutputs</span><span class="p">(</span><span class="n">numberOfOutputs</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="n">Extension</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="n">NeuralOutput</span> <span class="o">=</span> <span class="n">neural_output</span><span class="p">)</span>
   
    <span class="k">return</span> <span class="n">all_neuron_layer</span><span class="p">,</span> <span class="n">neural_output_element</span></div>


<div class="viewcode-block" id="get_super_cls_names"><span class="k">def</span> <span class="nf">get_super_cls_names</span><span class="p">(</span><span class="n">model_inst</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It returns the set of Super class of the model.</span>

<span class="sd">    Parameters</span>
<span class="sd">    -------</span>
<span class="sd">    model_inst :</span>
<span class="sd">        Instance of the scikit-learn model</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    parents : Set</span>
<span class="sd">        Returns all the parent class of the model instance.</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">super_cls_names</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
        <span class="k">nonlocal</span> <span class="n">parents</span>
        <span class="n">parents</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="vm">__name__</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">super_cls</span> <span class="ow">in</span> <span class="bp">cls</span><span class="o">.</span><span class="vm">__bases__</span><span class="p">:</span>
            <span class="n">super_cls_names</span><span class="p">(</span><span class="n">super_cls</span><span class="p">)</span>
    <span class="bp">cls</span> <span class="o">=</span> <span class="n">model_inst</span><span class="o">.</span><span class="vm">__class__</span>
    <span class="n">parents</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
    <span class="n">super_cls_names</span><span class="p">(</span><span class="bp">cls</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">parents</span></div>


<span class="kn">from</span> <span class="nn">nyoka</span> <span class="k">import</span> <span class="n">metadata</span>

<div class="viewcode-block" id="get_header"><span class="k">def</span> <span class="nf">get_header</span><span class="p">(</span><span class="n">description</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It returns the Header element of the pmml.</span>

<span class="sd">     Returns</span>
<span class="sd">     -------</span>
<span class="sd">     header :</span>
<span class="sd">         Returns Nyoka&#39;s Header object.</span>

<span class="sd">     &quot;&quot;&quot;</span>
    <span class="n">copyryt</span> <span class="o">=</span> <span class="s2">&quot;Copyright (c) 2018 Software AG&quot;</span>
    <span class="n">description</span> <span class="o">=</span> <span class="n">description</span> <span class="k">if</span> <span class="n">description</span> <span class="k">else</span> <span class="s2">&quot;Default Description&quot;</span>
    <span class="n">timestamp</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">())</span>
    <span class="n">application</span><span class="o">=</span><span class="n">pml</span><span class="o">.</span><span class="n">Application</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">&quot;Nyoka&quot;</span><span class="p">,</span><span class="n">version</span><span class="o">=</span><span class="n">metadata</span><span class="o">.</span><span class="n">__version__</span><span class="p">)</span>
    <span class="n">header</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">Header</span><span class="p">(</span><span class="n">copyright</span><span class="o">=</span><span class="n">copyryt</span><span class="p">,</span> <span class="n">description</span><span class="o">=</span><span class="n">description</span><span class="p">,</span> <span class="n">Timestamp</span><span class="o">=</span><span class="n">timestamp</span><span class="p">,</span> <span class="n">Application</span><span class="o">=</span><span class="n">application</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">header</span></div>


<div class="viewcode-block" id="get_dtype"><span class="k">def</span> <span class="nf">get_dtype</span><span class="p">(</span><span class="n">feat_value</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It return the data type of the value.</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    feat_value :</span>
<span class="sd">        Contains a value for finding the its data type.</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">        Returns the respective data type of that value.</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">data_type</span><span class="o">=</span><span class="n">feat_value</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span>
    <span class="k">if</span> <span class="s1">&#39;float&#39;</span> <span class="ow">in</span> <span class="n">data_type</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">DATATYPE</span><span class="o">.</span><span class="n">DOUBLE</span><span class="o">.</span><span class="n">value</span>
    <span class="k">if</span> <span class="s1">&#39;int&#39;</span> <span class="ow">in</span> <span class="n">data_type</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">DATATYPE</span><span class="o">.</span><span class="n">INTEGER</span><span class="o">.</span><span class="n">value</span>
    <span class="k">if</span> <span class="s1">&#39;str&#39;</span> <span class="ow">in</span> <span class="n">data_type</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">DATATYPE</span><span class="o">.</span><span class="n">STRING</span><span class="o">.</span><span class="n">value</span></div>

<div class="viewcode-block" id="get_data_dictionary"><span class="k">def</span> <span class="nf">get_data_dictionary</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">feature_names</span><span class="p">,</span> <span class="n">target_name</span><span class="p">,</span> <span class="n">categoric_values</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    It returns the Data Dictionary element.</span>
<span class="sd">    </span>
<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        A Scikit-learn model instance.</span>
<span class="sd">    feature_names : List</span>
<span class="sd">        Contains the list of feature/column name. </span>
<span class="sd">    target_name : List</span>
<span class="sd">        Name of the Target column.    </span>
<span class="sd">    categoric_values : tuple</span>
<span class="sd">        Contains Categorical attribute names and its values</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    data_dict :</span>
<span class="sd">        Returns Nyoka&#39;s DataDictionary object</span>
<span class="sd">        </span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">categoric_feature_name</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
    <span class="k">if</span> <span class="n">categoric_values</span><span class="p">:</span>
        <span class="n">categoric_labels</span> <span class="o">=</span> <span class="n">categoric_values</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">categoric_feature_name</span> <span class="o">=</span> <span class="n">categoric_values</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
    <span class="n">target_attr_values</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">n_features</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">feature_names</span><span class="p">)</span>
    <span class="n">features_pmml_optype</span> <span class="o">=</span> <span class="p">[</span><span class="n">OPTYPE</span><span class="o">.</span><span class="n">CONTINUOUS</span><span class="o">.</span><span class="n">value</span><span class="p">]</span> <span class="o">*</span> <span class="n">n_features</span>
    <span class="n">features_pmml_dtype</span> <span class="o">=</span> <span class="p">[</span><span class="n">DATATYPE</span><span class="o">.</span><span class="n">DOUBLE</span><span class="o">.</span><span class="n">value</span><span class="p">]</span> <span class="o">*</span> <span class="n">n_features</span>

    <span class="n">mining_func</span> <span class="o">=</span> <span class="n">get_mining_func</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">mining_func</span> <span class="o">==</span> <span class="n">MINING_FUNCTION</span><span class="o">.</span><span class="n">CLASSIFICATION</span><span class="o">.</span><span class="n">value</span><span class="p">:</span>
        <span class="n">target_pmml_optype</span> <span class="o">=</span> <span class="n">OPTYPE</span><span class="o">.</span><span class="n">CATEGORICAL</span><span class="o">.</span><span class="n">value</span>
        <span class="n">target_pmml_dtype</span> <span class="o">=</span> <span class="n">get_dtype</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">classes_</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
        <span class="n">target_attr_values</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">classes_</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
    <span class="k">elif</span> <span class="n">mining_func</span> <span class="o">==</span> <span class="n">MINING_FUNCTION</span><span class="o">.</span><span class="n">REGRESSION</span><span class="o">.</span><span class="n">value</span><span class="p">:</span>
        <span class="n">target_pmml_optype</span> <span class="o">=</span> <span class="n">OPTYPE</span><span class="o">.</span><span class="n">CONTINUOUS</span><span class="o">.</span><span class="n">value</span>
        <span class="n">target_pmml_dtype</span> <span class="o">=</span> <span class="n">DATATYPE</span><span class="o">.</span><span class="n">DOUBLE</span><span class="o">.</span><span class="n">value</span>

    <span class="n">data_fields</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>
    <span class="k">if</span> <span class="n">categoric_values</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">class_list</span><span class="p">,</span> <span class="n">attr_for_class</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">categoric_labels</span><span class="p">,</span> <span class="n">categoric_feature_name</span><span class="p">):</span>
            <span class="n">category_flds</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">DataField</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">attr_for_class</span><span class="p">),</span> <span class="n">optype</span><span class="o">=</span><span class="n">OPTYPE</span><span class="o">.</span><span class="n">CATEGORICAL</span><span class="o">.</span><span class="n">value</span><span class="p">,</span>
                                          <span class="n">dataType</span><span class="o">=</span><span class="n">get_dtype</span><span class="p">(</span><span class="n">class_list</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="k">if</span> <span class="n">class_list</span> <span class="k">else</span> <span class="n">DATATYPE</span><span class="o">.</span><span class="n">STRING</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">class_list</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">values</span> <span class="ow">in</span> <span class="n">class_list</span><span class="p">:</span>
                    <span class="n">category_flds</span><span class="o">.</span><span class="n">add_Value</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">Value</span><span class="p">(</span><span class="n">value</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">values</span><span class="p">)))</span>
            <span class="n">data_fields</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">category_flds</span><span class="p">)</span>
    <span class="n">attr_without_class_attr</span> <span class="o">=</span> <span class="p">[</span><span class="n">feat_name</span> <span class="k">for</span> <span class="n">feat_name</span> <span class="ow">in</span> <span class="n">feature_names</span> <span class="k">if</span> <span class="n">feat_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">categoric_feature_name</span><span class="p">]</span>
    <span class="k">for</span> <span class="n">feature_idx</span><span class="p">,</span> <span class="n">feat_name</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">attr_without_class_attr</span><span class="p">):</span>
        <span class="n">data_fields</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">DataField</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">feat_name</span><span class="p">),</span>
                                         <span class="n">optype</span><span class="o">=</span><span class="n">features_pmml_optype</span><span class="p">[</span><span class="n">feature_idx</span><span class="p">],</span>
                                         <span class="n">dataType</span><span class="o">=</span><span class="n">features_pmml_dtype</span><span class="p">[</span><span class="n">feature_idx</span><span class="p">]))</span>
    <span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span> <span class="ow">not</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;KMeans&#39;</span><span class="p">,</span> <span class="s1">&#39;IsolationForest&#39;</span><span class="p">,</span> <span class="s1">&#39;OneClassSVM&#39;</span><span class="p">]:</span>
        <span class="n">class_node</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">DataField</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">target_name</span><span class="p">),</span> <span class="n">optype</span><span class="o">=</span><span class="n">target_pmml_optype</span><span class="p">,</span>
                                <span class="n">dataType</span><span class="o">=</span><span class="n">target_pmml_dtype</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">class_value</span> <span class="ow">in</span> <span class="n">target_attr_values</span><span class="p">:</span>
            <span class="n">class_node</span><span class="o">.</span><span class="n">add_Value</span><span class="p">(</span><span class="n">pml</span><span class="o">.</span><span class="n">Value</span><span class="p">(</span><span class="n">value</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">class_value</span><span class="p">)))</span>
        <span class="n">data_fields</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">class_node</span><span class="p">)</span>
    <span class="n">data_dict</span> <span class="o">=</span> <span class="n">pml</span><span class="o">.</span><span class="n">DataDictionary</span><span class="p">(</span><span class="n">numberOfFields</span><span class="o">=</span><span class="nb">len</span><span class="p">(</span><span class="n">data_fields</span><span class="p">),</span> <span class="n">DataField</span><span class="o">=</span><span class="n">data_fields</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">data_dict</span></div>


<div class="viewcode-block" id="has_target"><span class="k">def</span> <span class="nf">has_target</span><span class="p">(</span><span class="n">model</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Checks whether a given model has target or not</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model :</span>
<span class="sd">        Scikit-learn&#39;s model object</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    Boolean value</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">target_less_models</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;OneClassSVM&#39;</span><span class="p">,</span><span class="s1">&#39;IsolationForest&#39;</span><span class="p">,</span> <span class="p">]</span>
    <span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span>  <span class="ow">in</span> <span class="n">target_less_models</span><span class="p">:</span>
        <span class="k">return</span> <span class="kc">False</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="kc">True</span></div>

</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2020, maintainer@nyoka.org

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>